具有授权的 Web API 不断提示登录
Web API with authorization keeps prompting for sign in
我正在尝试制作一个 ASP.NET Core Web API,它使用 Windows AD 组登录。基于 the documentation,我已经配置了授权和 Startup.ConfigureServices()
方法中的身份验证:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.AddPolicy("Reader", policy => policy.RequireRole(@"<DOMAIN>\<AD_ROLE_NAME>"));
});
services
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
services.AddControllers();
}
然后我将 [Authorize]
属性添加到我的控制器,以确保只有属于 AD 组的用户才能访问端点:
[Authorize(Policy = "Reader")]
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
如果我 运行 API(来自 Visual Studio)和 IIS, it seems to work fine. However, if I change it the execution method which has the same name as the API (I think it's Kestrel?),那么它会一直提示我登录。
如何让它与 Kestrel(或其他任何选项)一起使用?
您需要通过Configure方法在HTTP管道中配置认证授权:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
.
.
app.UseAuthentication(); // You need to setup the authentication before the authorization
app.UseAuthorization();
.
.
}
在 ConfigureServices
中,您已经使用 .AddAuthentication(NegotiateDefaults.AuthenticationScheme)
添加身份验证。在此方法中,用于注册服务的顺序无关紧要,因为我们只是为 DI 提供程序添加注册。
在Configure
中,顺序很重要,因为我们配置了 HTTP 管道,所以如果一个中间件添加在另一个中间件之前,那么它也会在它之前执行。
授权通过检查经过身份验证的用户的角色来工作,因此我们确实需要确保在授权之前完成身份验证过程。
当您的应用程序托管在 IIS 下时,我们有 IIS 负责身份验证,这就是它起作用的原因。在 launchSettings.json
你可能有这样的东西:
{
.
.
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
.
.
}
.
.
}
即配置IIS认证模块
我正在尝试制作一个 ASP.NET Core Web API,它使用 Windows AD 组登录。基于 the documentation,我已经配置了授权和 Startup.ConfigureServices()
方法中的身份验证:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.AddPolicy("Reader", policy => policy.RequireRole(@"<DOMAIN>\<AD_ROLE_NAME>"));
});
services
.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
services.AddControllers();
}
然后我将 [Authorize]
属性添加到我的控制器,以确保只有属于 AD 组的用户才能访问端点:
[Authorize(Policy = "Reader")]
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
如果我 运行 API(来自 Visual Studio)和 IIS, it seems to work fine. However, if I change it the execution method which has the same name as the API (I think it's Kestrel?),那么它会一直提示我登录。
如何让它与 Kestrel(或其他任何选项)一起使用?
您需要通过Configure方法在HTTP管道中配置认证授权:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
.
.
app.UseAuthentication(); // You need to setup the authentication before the authorization
app.UseAuthorization();
.
.
}
在 ConfigureServices
中,您已经使用 .AddAuthentication(NegotiateDefaults.AuthenticationScheme)
添加身份验证。在此方法中,用于注册服务的顺序无关紧要,因为我们只是为 DI 提供程序添加注册。
在Configure
中,顺序很重要,因为我们配置了 HTTP 管道,所以如果一个中间件添加在另一个中间件之前,那么它也会在它之前执行。
授权通过检查经过身份验证的用户的角色来工作,因此我们确实需要确保在授权之前完成身份验证过程。
当您的应用程序托管在 IIS 下时,我们有 IIS 负责身份验证,这就是它起作用的原因。在 launchSettings.json
你可能有这样的东西:
{
.
.
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
.
.
}
.
.
}
即配置IIS认证模块