Asp.net core 在ASP NET Core-404上启用基本身份验证
我使用这个中间件,因为这些概念对我来说是新的 我已经完全按照它所说的实施了启动 然而,每次我发布到WeatherForecast端点时,如果[Authorize]标识符在那里,我就会得到一个404。我做了一些阅读,因此添加了AuthenticationSchemes,但结果相同 它不能是丢失的重定向,这是另一个问题,因为控制器只是返回该页面中的内容 有人能给我指一下正确的方向吗Asp.net core 在ASP NET Core-404上启用基本身份验证,asp.net-core,basic-authentication,Asp.net Core,Basic Authentication,我使用这个中间件,因为这些概念对我来说是新的 我已经完全按照它所说的实施了启动 然而,每次我发布到WeatherForecast端点时,如果[Authorize]标识符在那里,我就会得到一个404。我做了一些阅读,因此添加了AuthenticationSchemes,但结果相同 它不能是丢失的重定向,这是另一个问题,因为控制器只是返回该页面中的内容 有人能给我指一下正确的方向吗 public void ConfigureServices(IServiceCollection services)
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");
}
}
[路由(“[控制器]”)]
[授权(AuthenticationSchemes=idunno.Authentication.Basic.BasicAuthenticationDefaults.AuthenticationScheme)]
公共类WeatherForecastController:ControllerBase
{
[HttpGet]
公共操作控制器Get()
{
退货内容(“授权”);
}
}
根据注释,一些命令的顺序错误根据注释,一些命令的顺序错误您是否在开发
模式下运行此操作?是-应该是什么?如果将使用身份验证
和使用授权
对放在正确的位置,会发生什么?i、 e.介于UseRouting
和UseEndpoints
之间?非常感谢,它现在可以工作了!您是否在Development
模式下运行此功能?是-应该是什么?如果将UseAuthentication
和UseAuthorization
对放在正确的位置,会发生什么情况?i、 e.介于UseRouting
和UseEndpoints
之间?非常感谢,它现在可以工作了!