簡體   English   中英

Azure活動目錄| 基於角色的授權

[英]Azure Active Directory | Roles based authorization

我已經嘗試了Internet上Azure Active Directory的各種身份驗證方案。 所有示例僅集中於通過身份驗證進行授權。 我一直在根據我的AAD App Registration角色授權用戶。

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可以連接任何middleware來處理傳入的http請求。 Auth Middlewares例如OpenIdWindowsBearerToken ,...

UseOpenIdConnectAuthentication()的唯一正確middleware超過按角色授權網絡資源UseWindowsAzureActiveDirectoryBearerAuthentication()根據這個例子嗎?

請提出建議。

是的,OpenID是唯一適用於此的中間件。 在這一點上,沒有其他選擇可以替代OpenID Connect。

我發現設置角色的最佳方法是在清單中添加這些角色,然后對邏輯進行硬編碼以為不同的用戶提供不同的權限。

這是到目前為止我找到的最好的樣本。 您只需將連接字符串添加到Azure SQL即可正常工作。 https://github.com/Azure-Samples/active-directory-dotnet-webapp-roleclaims

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM