【问题标题】:How do i get this Basic Authentication working in .NET Core我如何让这个基本身份验证在 .NET Core 中工作
【发布时间】:2018-05-25 18:09:08
【问题描述】:

我正在使用Basic Authentication,并为此创建了Middleware。当我使用 Authorize 属性并尝试通过 Postman 进行 API 调用时,即使我在 Postman 中添加了授权详细信息,我也会得到 401 Unauthorize

我不确定这是我通过邮递员调用控制器操作的方式,还是我在邮递员中缺少标题选项。

配置服务

        services
            .AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
            .AddBasicAuthentication(GetBasicAuthenticationOptions(users));

配置

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
        app.UseCors("SiteCorsPolicy");
        app.UseAuthentication();
    }

基本身份验证实现

    private Action<BasicAuthenticationOptions> GetBasicAuthenticationOptions(IList<UserConfiguration> users)
    {
        return options =>
        {
            options.Realm = "Public API";
            options.Events = new BasicAuthenticationEvents
            {
                OnValidatePrincipal = context =>
                {
                    var authenticatedUser = users.FirstOrDefault(u =>
                        u.Username == context.UserName && u.Password == context.Password);

                    if (authenticatedUser != null)
                    {
                        var claims = new List<Claim>
                        {
                            new Claim(ClaimTypes.Name,
                                context.UserName,
                                context.Options.ClaimsIssuer)
                        };

                        var principal = new ClaimsPrincipal(new ClaimsIdentity(claims,
                            BasicAuthenticationDefaults.AuthenticationScheme));
                        context.Principal = principal;

                        return Task.CompletedTask;
                    }

                    return Task.FromResult(AuthenticateResult.Fail("Authentication failed."));
                }
            };
        };
    }

带有授权的控制器操作

    [Authorize]
    [HttpPost]
    [Route("test")]
    public IActionResult Test()
    {
        return Json("yes");
    }

邮递员请求

用户名和密码在 appsettings.json 文件中

【问题讨论】:

  • @KirkLarkin services.AddAuthentication 是中间件代码,在 Startup.cs 的 Configure 方法里面有一个 UseAuthentication
  • 不要那样做!您正在存储和比较未散列的密码!请改用 ASP.NET Core 自己的机制。此代码几乎可以保证您或您客户的网站将被破坏
  • 虽然和你问的问题无关,但是你会发现很多SO用户不会愿意帮你写这么不安全的代码。
  • @ifelabolz Kirk Larkin 已经以迂回的方式回答了这个问题。不要创建自己的。使用像 idunno.Authentication 这样的提供者。作者在 .NET 的安全团队中,他确实说“你真的不应该这样做”。这样您就可以避免中间件本身出现问题,并且只需要解决安全密码存储和验证问题。
  • @ifelabolz 如果您不提供所有代码或不提及您使用的软件包,则无法尝试和重现。无论如何,您是否尝试过 debugging ?这段代码有什么作用,它的行为如何?你是说那个包有错误吗?您是否能够在该存储库中运行测试项目?

标签: asp.net-mvc asp.net-core basic-authentication authorize-attribute


【解决方案1】:

问题只是因为 app.UseMvc(); 放在 app.UseAuthentication(); 之前。我只需要在app.UseAuthentication(); 之后放置app.UseMvc(); 并修复它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 2021-01-04
    • 2019-01-28
    • 2013-07-31
    • 2018-12-17
    相关资源
    最近更新 更多