【问题标题】:Enabling basic authentication on ASP NET Core - 404在 ASP NET Core 上启用基本身份验证 - 404
【发布时间】:2020-03-18 04:22:04
【问题描述】:

我正在使用这个中间件,因为这些概念对我来说是新的

https://github.com/blowdart/idunno.Authentication/tree/master/src/idunno.Authentication.Basic

我已经完全按照它所说的方式实现了启动

但是,每次我发布到 WeatherForecast 端点时,如果 [Authorize] 标识符存在,我都会收到 404。我做了一些阅读,因此添加了 AuthenticationSchemes 但结果相同

不可能是缺少重定向,这是另一个问题,因为控制器只是返回该页面中的内容?

请有人指出正确的方向吗?

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddMvc();
        services.AddRazorPages();
        services.AddControllersWithViews();

        services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
        .AddBasic(options =>
        {
            //options.Realm = "idunno";

            options.Events = new BasicAuthenticationEvents
            {
                OnValidateCredentials = context =>
                {
                    if (context.Username == context.Password)
                    {
                        var claims = new[]
                        {
                            new Claim(
                                ClaimTypes.NameIdentifier,
                                context.Username,
                                ClaimValueTypes.String,
                                context.Options.ClaimsIssuer),
                            new Claim(
                                ClaimTypes.Name,
                                context.Username,
                                ClaimValueTypes.String,
                                context.Options.ClaimsIssuer)
                        };

                        context.Principal = new ClaimsPrincipal(
                            new ClaimsIdentity(claims, context.Scheme.Name));
                        context.Success();
                    }

                    return Task.CompletedTask;
                }
            };
        });
    }

那么这个

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }

控制器如下所示:

[Route("[controller]")]
[Authorize(AuthenticationSchemes = idunno.Authentication.Basic.BasicAuthenticationDefaults.AuthenticationScheme)]
public class WeatherForecastController : ControllerBase
{
    [HttpGet]
    public ActionController<WeatherForecast> Get()
    {
        return Content("Authorised"); 
    }
}

【问题讨论】:

  • 你是在Development模式下运行的吗?
  • 是的 - 它应该是什么?
  • 如果将UseAuthenticationUseAuthorization 对放在正确的位置会发生什么?即在UseRoutingUseEndpoints之间?
  • 非常感谢,现在可以使用了!

标签: asp.net-core basic-authentication


【解决方案1】:

根据评论,某些命令的顺序错误

【讨论】:

    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2017-09-11
    • 2023-04-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多