在Asp.Net Core 3.0中禁用身份验证以供开发使用

3
如何在具有[Authorize]属性的控制器开发期间禁用身份验证? 这是一个针对 .NET Core 2 的答案,但它使用不再适用于 .NET Core 3.0 的AddMvc()方法。请参考此处
我尝试了以下方法:
    services.AddControllers().AddMvcOptions(opts => opts.Filters.Add<AllowAnonymousFilter>());

它仍然返回401;我不知道这是否是正确的方向。

更新:

之前链接的帖子已经更新,其中包含适用于3.x版本的答案。

在开发环境中禁用Asp.net身份验证


services.AddMvc() 在 .net core 3.0 中属于 MvcServiceCollectionExtensions 扩展。您可以使用与 .net core 2 相同的方法来实现所需功能。 - Venkata Dorisala
@Venky 它实际上并不起作用,可能是因为应用程序的其余部分没有按照 AddMvc 模式设置。即使它能够工作,我也会感到不舒服,因为 AddMvc 似乎不再是推荐的模式,并且在这种情况下已被 AddControllers 取代。 - stellr42
1
[AllowAnonymous]过滤器应用作为简化开发的方式实际上并不是一个好主意,因为您在提供用户实际认证时跳过了应用程序的重要部分。因此,最好的做法是登录一个包含所需声明的合成用户,并使授权按预期工作。 - poke
3个回答

2
只需转到项目中的launchSettings.json文件: 1 然后将 "anonymousAuthentication" 设置为 "true"。

1
你可以尝试这样做。
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方法的解决方案。 - stellr42

0

在开发过程中,使用“test”声明信息自动登录用户如何?例如,假设在非开发环境中,您使用以下内容来授权用户:

// 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。 - stellr42
也许我漏掉了什么。如果您能够绕过您不控制的项目的身份验证,那不是一个很大的安全问题吗?您是想利用漏洞吗?向另一个项目的创建者请求创建角色或添加简单逻辑以供开发是否更好? - CodeCaptain
我控制我的项目,但它不会添加索赔或创建用户。我的项目只从 cookie 中读取现有索赔,这不需要任何基于索赔的工作。因此,我不想在我的项目中开始添加不相关的索赔代码。 - stellr42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接