【问题标题】:Disable authentication in Asp.Net Core 3.0 for development在 Asp.Net Core 3.0 中禁用身份验证以进行开发
【发布时间】:2020-08-26 22:16:41
【问题描述】:

如何在开发过程中为具有[Authorize] 属性的控制器禁用身份验证? Here 是 .net core 2 的答案,但它使用了 AddMvc(),这在 .net core 3.0 中没有使用。

我试过了:

    services.AddControllers().AddMvcOptions(opts => opts.Filters.Add<AllowAnonymousFilter>());

它仍然返回 401;我不知道这是否在正确的轨道上。

更新:

之前链接的帖子已更新为适用于 3.x 的答案。

Asp.net "disable" authentication in development environment

【问题讨论】:

  • services.AddMvc() 位于 .net core 3.0 中,扩展名为 MvcServiceCollectionExtensions。您可以使用与.net core 2 相同的方法来实现您所需要的。
  • @Venky 它实际上不起作用,可能是因为应用程序的其余部分没有以AddMvc 模式设置。即使是这样,我使用它也会感到不舒服,因为AddMvc 似乎不再是推荐的模式,并且在这种情况下已被AddControllers 取代。
  • 应用[AllowAnonymous] 过滤器来简化开发确实不是一个好主意,因为您正在跳过应用程序的重要部分,而提供您可能依赖的信息在用户实际通过身份验证时开启。因此,最好以包含您需要的声明并根据需要进行授权工作的综合用户身份登录。

标签: c# asp.net-core .net-core asp.net-core-3.0


【解决方案1】:

只需转到项目中的 launchSettings.json 即可:

然后将“anonymousAuthentication”设置为“true”。

【讨论】:

    【解决方案2】:

    你可以试试这样的。

    public class Startup 
    {
       public Startup(IConfiguration configuration, IWebHostEnvironment env)
       {
                Configuration = configuration;
                Environment = env;
       }
    
       public Microsoft.AspNetCore.Hosting.IWebHostEnvironment Environment { get; }
    
       public void ConfigureServices(IServiceCollection services)
       {
                services.AddControllers(opts =>
                {
                    if (Environment.IsDevelopment())
                    {
                        opts.Filters.Add<AllowAnonymousFilter>();
                    }
                    else
                    {
                      var authenticatedUserPolicy = new AuthorizationPolicyBuilder()
                                .RequireAuthenticatedUser()
                                .Build();
                      opts.Filters.Add(new AuthorizeFilter(authenticatedUserPolicy)); 
                     }
                });
        }
    
    } 
    

    【讨论】:

    • 谢谢,这确实有效。我唯一的问题是它将授权应用于每个控制器,而不是允许更细粒度地控制在何处应用 [Authorize] 属性。我仍然希望有一个更类似于 .net core 2 方法的解决方案。
    【解决方案3】:

    如何在开发时使用“测试”声明信息自动登录用户。例如,假设您在非开发环境中使用如下方式授权用户:

    // Checked the database and user is legit so populate the claims
    // Create the identity for the user. userList is var or list populated from database. userEmail is the user's email or some other identifier.
    identity = new ClaimsIdentity(new[] {
        new Claim(ClaimTypes.Name, userList.fullname),
        new Claim(ClaimTypes.Role, userList.userrole),
        new Claim(ClaimTypes.NameIdentifier, userEmail),
    }, CookieAuthenticationDefaults.AuthenticationScheme);
    
    var principal = new ClaimsPrincipal(identity);
    var login = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
    return RedirectToAction("Index", "Home");
    

    当您处于开发阶段时,您可以执行以下操作:

    // You may need to inject Microsoft.AspNetCore.Hosting.IHostingEnvironment. I use .Net core 2.2 so not sure about 3.
    if (env.EnvironmentName == "Development")
    {
        // In Development so create "test" claim information and automatically authorize the user
        // Create the identity for the user
        identity = new ClaimsIdentity(new[] {
        new Claim(ClaimTypes.Name, "Test User"),
        new Claim(ClaimTypes.Role, "Tester"),
        new Claim(ClaimTypes.NameIdentifier, "tester@test.com"),
        }, CookieAuthenticationDefaults.AuthenticationScheme);
    
        // Populate the session user name
        HttpContext.Session.SetString(SessionUserName, userList.fullname);
    
        var principal = new ClaimsPrincipal(identity);
        var login = HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
        return RedirectToAction("Index", "Home");
    }
    
    

    【讨论】:

    • 我不想这样做,因为身份验证由不同的项目处理;这个项目只是一个消费者。我正在寻找一种方法来简单地绕过[Authorize] 属性,如.net core 2 的链接问题所示。
    • 也许我错过了什么。如果您能够绕过您无法控制的项目的身份验证,这不是一个大的安全问题吗?您是否希望利用漏洞?让其他项目的创建者创建角色或添加简单的开发逻辑会更好吗?
    • 我控制着我的项目,但它不做任何添加声明或创建用户的工作。我的项目仅从 cookie 中读取 现有 声明,这不需要任何基于声明的工作。因此,我不想开始在我的项目中添加不相关的声明代码。
    猜你喜欢
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2020-01-30
    • 2017-01-29
    • 2020-01-25
    • 2019-04-30
    相关资源
    最近更新 更多