Windows 身份验证 - Postman(HttpContext 名称为 Null)
Windows Authentication - Postman (HttpContext Name is Null)
我的问题与Postman Windows Authentication (NTLM) not working类似,但目前还没有答案。
我用过.NetCore rest api (netcoreapp3.1).
在launchsettings.json
{
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
..
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(IISDefaults.AuthenticationScheme);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
//....useEndpoints middleware is being called afterwards
}
EmployeeController.cs
[Route("IISDetails")]
[HttpGet]
public IActionResult IISDetails()
{
var name = User.Identity.Name;
return new ContentResult() { Content = $@"IIS authorized. AD: {name}" };
}
我没有使用 [Authorize] 标签,所以至少我可以看看这是否有效,但 Name 始终为 null。
邮递员我在授权选项卡中设置用户名。附上图片供参考。
如果我将 Authorize 属性放入我的 IISDetails 函数,它会给我
System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
所以我添加了 [Authorization] 标签并将 Startup.cs 从
更改为
services.AddAuthentication(IISDefaults.AuthenticationScheme);
到
services.AddAuthentication(Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme).AddNegotiate();
在浏览器中试了一下,成功了!!
P.S:仍然无法使用 Postman NTLM 身份验证(测试版)
- 在IISDefaults.AuthenticationScheme,它给了我
System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions).
at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties)
at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
仍然感谢任何关于为什么其他事情不起作用的指导:)
我的问题与Postman Windows Authentication (NTLM) not working类似,但目前还没有答案。
我用过.NetCore rest api (netcoreapp3.1).
在launchsettings.json
{ "iisSettings": { "windowsAuthentication": true, "anonymousAuthentication": false, .. }
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(IISDefaults.AuthenticationScheme); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //... app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); //....useEndpoints middleware is being called afterwards }
EmployeeController.cs
[Route("IISDetails")] [HttpGet] public IActionResult IISDetails() { var name = User.Identity.Name; return new ContentResult() { Content = $@"IIS authorized. AD: {name}" }; }
我没有使用 [Authorize] 标签,所以至少我可以看看这是否有效,但 Name 始终为 null。
邮递员我在授权选项卡中设置用户名。附上图片供参考。
如果我将 Authorize 属性放入我的 IISDetails 函数,它会给我
System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found.
所以我添加了 [Authorization] 标签并将 Startup.cs 从
更改为services.AddAuthentication(IISDefaults.AuthenticationScheme);
到
services.AddAuthentication(Microsoft.AspNetCore.Authentication.Negotiate.NegotiateDefaults.AuthenticationScheme).AddNegotiate();
在浏览器中试了一下,成功了!!
P.S:仍然无法使用 Postman NTLM 身份验证(测试版)
- 在IISDefaults.AuthenticationScheme,它给了我
System.InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action configureOptions). at Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, String scheme, AuthenticationProperties properties) at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
仍然感谢任何关于为什么其他事情不起作用的指导:)