我在asp.net核心2.0中创建了应用程序接口,其中我使用混合模式身份验证。对于一些控制器,JWT和一些使用windows身份验证的控制器。
我对使用JWT授权的控制器没有问题。但是对于要使用windows身份验证的控制器,系统会无限期地提示我使用chrome的用户名和密码对话框。
下面是我希望使用Windows身份验证而不是JWT的示例控制器代码。
[Route("api/[controller]")]
[Authorize(AuthenticationSchemes = "Windows")]
public class TestController : Controller
{
[HttpPost("processUpload")]
public async Task<IActionResult> ProcessUploadAsync(UploadFileModel uploadFileModel)
{
}
}
我的配置服务代码
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IISDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer("Bearer", options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateAudience = false,
ValidateIssuer = false,
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("blahblahKey")),
ValidateLifetime = true, //validate the expiration and not before values in the token
ClockSkew = TimeSpan.FromMinutes(5) //5 minute tolerance for the expiration date
};
});
// let only my api users to be able to call
services.AddAuthorization(auth =>
{
auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.RequireClaim(ClaimTypes.Name, "MyApiUser").Build());
});
services.AddMvc();
}
我的配置方法。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("CorsPolicy");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication(); //needs to be up in the pipeline, before MVC
app.UseMvc();
}
感谢您在这方面的建议和帮助。
更新:到目前为止,我一直在chrome上调试我的代码。但是当我使用IE11时,上面的代码运行没有任何问题。
这可能是CORS问题的chrome在哪里印前问题?
谢谢
发布于 2020-02-26 00:23:37
我也有同样的需求。我还没有在IIS上运行东西,只在Kestrel上运行,但我设法调整了微软的own instructions,以便使用JWT和Windows auth获得每个控制器/控制器方法的身份验证。
我所做的只是修改Startup.cs/ConfigureServices
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false; // should be set to true in production
x.SaveToken = true;
x.TokenValidationParameters = generateTokenValidationParameters();
});
到这个
services.AddAuthentication()
.AddNegotiate()
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false; // should be set to true in production
x.SaveToken = true;
x.TokenValidationParameters = generateTokenValidationParameters();
});
因此,基本上,删除了默认的身份验证和挑战方案,添加了使用我预先存在的JWT配置的协商(Windows Auth)和JwtBearer。
在控制器中,我通过添加此授权头启用了Windows身份验证
[Authorize(AuthenticationSchemes = NegotiateDefaults.AuthenticationScheme)]
类似地,我用下面的代码替换了以前支持JWD的现有Authorization头(假设它是默认的身份验证/质询方案
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
我的应用程序将由Kestrel托管,因此IIS不会成为问题,但我接下来将尝试使用它。
@edit:我现在也掌握了IIS Express。通过VS的IIS Express设置(项目属性,调试选项卡)启用Windows Auth,然后通过将其添加到Startup.ConfigureServices (紧跟在AddAuthentication之后)来确保IIS不会对进程内和进程外主机执行自动身份验证。
//disable automatic authentication for in-process hosting
services.Configure<IISServerOptions>(options =>
{
options.AutomaticAuthentication = false;
});
//disable automatic authentication for out-of-process hosting
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = false;
});
然后,我将我的测试控制器更改为具有以下authorize标头的方法
[Authorize(AuthenticationSchemes = IISDefaults.AuthenticationScheme)]
当我使用信任URL的浏览器访问该方法时,我被允许进入,而User.Identity是我的windows标识。
现在,让我们来看看这是否也适用于实际的IIS。
https://stackoverflow.com/questions/51053459
复制相似问题