gpt4 book ai didi

c# - Azure 事件目录 |基于角色的授权

转载 作者:行者123 更新时间:2023-12-03 05:47:24 30 4
gpt4 key购买 nike

我已经尝试了跨互联网的 Azure Active Directory 的各种身份验证方案。所有示例仅关注通过身份验证进行授权。我正在寻找根据我的 AAD 应用注册 中的角色对用户进行授权。

Auth() Scenarios ,

例如,

..\Controller\ArtistController.cs:

public class ArtistController : ApiController
{
[Authorize(Roles = "Admin, InternalAdmin")]
public void Post(ArtistModel model)
{
// Do admin stuff here...
}
}

..\App_Start\Startup.Auth.cs [不工作]:

public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
TokenValidationParameters = new TokenValidationParameters
{
SaveSigninToken = true,
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"],
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
}
});
}

..\App_Start\Startup.Auth.cs [工作]:

public void ConfigureAuth(IAppBuilder app)
{
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = ConfigHelper.ClientId,
Authority = ConfigHelper.Authority,
RedirectUri = "<<Home_Url>>",
PostLogoutRedirectUri = ConfigHelper.PostLogoutRedirectUri,

TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
NameClaimType = "upn",
RoleClaimType = "roles", // The claim in the Jwt token where App roles are provided.
},

Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthenticationFailed = context =>
{
context.HandleResponse();
context.Response.Redirect("/Error/ShowError?signIn=true&errorMessage=" + context.Exception.Message);
return System.Threading.Tasks.Task.FromResult(0);
}
}
});
}

据我了解,OWIN 可以连接任何中间件来处理传入的http 请求。 Auth 中间件,如 OpenIdWindowsBearerToken、...

基于此示例,UseOpenIdConnectAuthentication() 是唯一正确的通过 UseWindowsAzureActiveDirectoryBearerAuthentication() 按角色授权 Web 资源的中间件吗?

请提出建议。

最佳答案

是的,OpenID 是唯一适用于此目的的中间件。目前没有 OpenID Connect 的替代方案。

我发现设置角色的最佳方法是在 list 中添加这些角色,然后对逻辑进行硬编码以向不同的用户授予不同的权限。

这是迄今为止我找到的最好的示例。您只需将连接字符串添加到 Azure SQL 即可使其工作。 https://github.com/Azure-Samples/active-directory-dotnet-webapp-roleclaims

关于c# - Azure 事件目录 |基于角色的授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492438/

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