gpt4 book ai didi

c# - .NET Core 2 - Web Api - Azure AD - 用户为 NULL

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:56 25 4
gpt4 key购买 nike

我有一个 .NET Core 2.1 Web API 应用程序,我在其中设置了 Azure AD 以进行身份​​验证。对于我的授权,我使用了一个 AuthorizationHandler,它根据用户名进行一些验证。我遇到的问题是 User.Identity.Name 总是返回 null,我不确定为什么。

这是我的 Startup.cs,我在其中设置了身份验证和授权:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}

app.UseCors("AllowAnyOrigin");

app.UseAuthentication();

app.UseMvc();
}

public void ConfigureServices(IServiceCollection services)
{
// Azure AD
services.AddAuthentication(
sharedOptions =>
{
sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(
options =>
{
options.Authority = "https://login.microsoftonline.com/" + "MyTenant";
options.Audience = "MyClientId";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "https://login.microsoftonline.com/" + "MyTenant" +"/v2.0";
};
});

services.AddAuthorization(
options =>
{
options.AddPolicy("MyPolicy", policy => policy.Requirements.Add(new NameRequirement());
});

services.AddSingleton<IAuthorizationHandler, NameRequirementHandler>();

services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});

services.Configure<MvcOptions>(options => {
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAnyOrigin"));
});



services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

在我的 AuthorizationHandler 中,我想获取经过身份验证的用户的名称,以便对其进行一些验证。我可以在请求中看到 Bearer token ,但是 User.Identity.Name 和所有声明都是 null

public class NameRequirementHandler : AuthorizationHandler<NameRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, NameRequirement requirement)
{
var authorizationFilterContext = context.Resource as AuthorizationFilterContext;
if (authorizationFilterContext != null)
{
HttpContext httpContext = authorizationFilterContext.HttpContext;
string userName = httpContext.User.Identity.Name; // ALWAYS NULL

//Do some validation here with the user name value
}

context.Succeed(requirement);

return Task.CompletedTask;
}
}

目前我有一个非常简单的 Controller ,我用它来测试身份验证和授权:

[Authorize(Policy = "MyPolicy")]
public class ValuesController
{
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
}

为了从不记名 token 中填充用户信息,我缺少什么?为了添加更多上下文,我将它从 .NET Framework 移植过来,它在那里工作,但它使用扩展方法 UseWindowsAzureActiveDirectoryBearerAuthentication,它在 .NET Core 中不存在

这是它在 .NET Framework 中的样子

public void Configuration(IAppBuilder app)
{
var tokenValidation = new TokenValidationParameters
{
ValidAudience = "https://graph.windows.net/"
};

var authOptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = "MyTenant",
TokenValidationParameters = tokenValidation,
MetadataAddress = ""
};

app.UseWindowsAzureActiveDirectoryBearerAuthentication(authOptions);
}

我已经看到其他问题已发布并阅读了很多文章,但仍然无法使它起作用。我的配置的哪一部分是错误的?

提前致谢。

已更新

我最终找到了这个问题的解决方案,受众值(value)设置不正确。我没有设置 token 的“aud”值的受众值(value)。你可以去https://jwt.ms查看 token 的声明并确保设置了正确的受众。

最佳答案

您需要在您有权访问 IApplicationBuilder 的 Configure 方法中调用 UseAuthentication。它会是这样的,

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseAuthentication(); // Make sure you've done this!

app.UseMvc();
}

从文档中,“将 AuthenticationMiddleware 添加到指定的 IApplicationBuilder,从而启用身份验证功能。”

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.authappbuilderextensions.useauthentication?view=aspnetcore-2.0

关于c# - .NET Core 2 - Web Api - Azure AD - 用户为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52728364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com