小编Joa*_*iro的帖子

在使用 testcontainers 的 docker 中运行 dotnet 测试

我有一个集成测试项目,它在 VS 中按预期执行。集成测试使用 MsSql 测试容器(来自https://dotnet.testcontainers.org/)。

我的目标是在 Docker 映像内的 Azure DevOps 管道中运行这些测试,就像我在其他不使用测试容器的项目中成功执行的那样。现在我只是尝试在本地计算机的 docker 映像中运行测试。不幸的是我面临连接问题。

我的环境:

  • .NET 6
  • 操作系统:Windows
  • 带有 Linux 容器的 Docker 桌面

我的代码:

Authentication.Api/MyProject.Authentication.Api/Dockerfile:

##########################################################
# build

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Authentication.Api/MyProject.Authentication.Api/MyProject.Authentication.Api.csproj", "Authentication.Api/MyProject.Authentication.Api/"]
COPY ["Authentication.Api/MyProject.Authentication.Api.IntegrationTests/MyProject.Authentication.Api.IntegrationTests.csproj", "Authentication.Api/MyProject.Authentication.Api.IntegrationTests/"]
RUN dotnet restore "Authentication.Api/MyProject.Authentication.Api/MyProject.Authentication.Api.csproj"
RUN dotnet restore "Authentication.Api/MyProject.Authentication.Api.IntegrationTests/MyProject.Authentication.Api.IntegrationTests.csproj"
COPY . .

WORKDIR "/src/Authentication.Api/MyProject.Authentication.Api"
RUN dotnet build "MyProject.Authentication.Api.csproj" -c Release -o /app/build

WORKDIR "/src/Authentication.Api/MyProject.Authentication.Api.IntegrationTests"
RUN dotnet build -c Release

##########################################################
# run test projects

FROM build AS tests
WORKDIR /src …
Run Code Online (Sandbox Code Playgroud)

.net docker dockerfile testcontainers

8
推荐指数
2
解决办法
3447
查看次数

如何使用 IdentityUser 忽略 AspNetUserClaims 表?

我想使用 IdentityUser,但我不想使用声明或登录。因此,我想从我的数据库中删除这些表。基于一些搜索,我实现了我的数据库上下文(代码优先),如下所示:

public class MainDb : IdentityDbContext
{
    (...)

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("User")
            .Ignore(x => x.AccessFailedCount)
            .Ignore(x => x.Claims)
            .Ignore(x => x.EmailConfirmed)
            .Ignore(x => x.LockoutEnabled)
            .Ignore(x => x.LockoutEndDateUtc)
            .Ignore(x => x.Logins)
            .Ignore(x => x.PhoneNumberConfirmed)
            .Ignore(x => x.TwoFactorEnabled);

        modelBuilder.Ignore<IdentityUserLogin>();
        modelBuilder.Ignore<IdentityUserClaim>();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试创建迁移(程序包管理器控制台中的“添加迁移用户”)时,出现以下错误:

The navigation property 'Claims' is not a declared property on type 'IdentityUser'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.
Run Code Online (Sandbox Code Playgroud)

如果我删除引用声明和登录的行,则会创建迁移,并忽略其他属性:

public class …
Run Code Online (Sandbox Code Playgroud)

asp.net ef-code-first asp.net-identity

6
推荐指数
0
解决办法
1400
查看次数

MethodInfo.GetCustomAttribute 上的 FileNotFoundException

我的目标是显示有关上传的 dll 中某些类的方法的信息。加载程序集、找到所需的类及其方法已经成功完成。现在我试图显示一个方法是否被声明为“异步”。

我发现一个线程告诉我如何做到这一点:How can I Tell if a C# method is async/await via Reflection?

不管怎样,在测试时,当我打电话时

(AsyncStateMachineAttribute)methodInfo.GetCustomAttribute(typeof(AsyncStateMachineAttribute))

我收到 System.IO.FileNotFoundException -“无法加载文件或程序集“{程序集标识符}”或其依赖项之一。系统找不到指定的文件。”。

我在一个未答复的线程中发现了此异常,但它对我没有帮助:How to Prevent MemberInfo.IsDefined from throwing FileNotFoundException on irrelevant attribute?

我知道我正在查看的方法有一个我的代码不知道的属性。我不想加载该引用,因为它只是一个测试用例,在相同的情况下可以找到许多其他不同的属性。

因此,我需要回答以下两个问题之一:

  1. 有没有办法获取属性“AsyncStateMachineAttribute”(如果存在),并忽略其他属性上的错误?

  2. 是否有另一种方法来检查方法(来自 MethodInfo)是否是异步的?

提前致谢!:)

.net c# reflection asynchronous system.reflection

5
推荐指数
0
解决办法
574
查看次数

asp.net核心中的相关性失败

我的Web应用程序无法使用OpenIdConnect进行身份验证.目前我看到"关联失败"错误OnRemoteFailure.

语境:

  • Service Fabric无状态.net核心Web应用程序
  • Azure B2C
  • 反向代理(Traefik)

启动:

    public void ConfigureServices(IServiceCollection services)
    {
        (...)

        services.AddMvc();

        (...)

        services.AddAuthorization();
        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddOpenIdConnect(o =>
        {
            o.ClientId = clientId;
            o.Authority = $"https://login.microsoftonline.com/{tenantId}/{signinPolicy}/v2.0";
            o.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            o.SaveTokens = true;
            o.Events = new OpenIdConnectEvents
            {
                OnTokenValidated = async context =>
                {
                    (...)
                },
                OnRedirectToIdentityProvider = async context =>
                {
                    if (context.Request.Headers.TryGetValue("X-Forwarded-Prefix", out var prefix) && prefix.Count > 0 && …
Run Code Online (Sandbox Code Playgroud)

openid-connect azure-service-fabric azure-ad-b2c asp.net-core traefik

5
推荐指数
1
解决办法
653
查看次数

ASP.NET MVC 成功登录后未登录

在我自己的机器和浏览器 (chrome) 中,我无法登录我的网站。它适用于其他浏览器、其他 chrome 用户和隐身窗口。它也适用于我的开发环境或同一网站的其他阶段。

我关于登录的相关代码如下:

=> StartUp.ConfigureAuth

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, Data.DbContainer.Entities.User>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});            
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
Run Code Online (Sandbox Code Playgroud)

=> 登录端点

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    SignInStatus result;
    using (var mainDb = MainDbDataManager.GetInstance())
    {
        var user = await mainDb.UserManager.FindByNameAsync(model.Username);
        // Check if user has permission to access …
Run Code Online (Sandbox Code Playgroud)

cookies asp.net-mvc owin

2
推荐指数
1
解决办法
1312
查看次数

避免从函数应用程序中记录应用程序见解中的严重性级别 0

在我的 Azure 解决方案中,我有 1 个应用程序服务和 2 个功能应用程序记录到 1 个应用程序洞察实例。在特定环境中,我想减少日志记录负载,因此我想摆脱严重性级别 0 的日志。

我目前专注于其中一个功能应用程序,我们将其称为 fa1。我使用 ILogger 添加为 LogDebug 的日志记录语句不会按预期显示在应用程序见解中。但是我可以在应用程序见解中看到以下条目:

  • “使用 ClientRequestId '{...}' 对队列 '{...}' 上的函数 '{fa1 中的函数名称}' 进行轮询,在 5 毫秒内发现 0 条消息。”
  • “函数‘{fa1 中的函数名称}’将等待 60000 毫秒,然后再轮询队列‘{...}’。”

我还看到以下条目,但我不知道哪个服务正在生成它们:

  • “[HostMonitor] 主机进程 CPU 统计信息:EffectiveCores=1,CpuLoadHistory=(0,0,0,0,0,0,0,0,0,0),AvgCpuLoad=0,MaxCpuLoad=0”
  • “[HostMonitor] 主机聚合 CPU 负载 0”

主机.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    },
    "logLevel": {
      "Function": "Warning",
      "default": "Warning"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

启动.配置():

builder.Services.AddLogging(loggingBuilder =>
{
    var key = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
    loggingBuilder.AddApplicationInsights(key);
});
builder.Services.AddSingleton(sp …
Run Code Online (Sandbox Code Playgroud)

azure azure-application-insights azure-function-app

2
推荐指数
1
解决办法
1531
查看次数