【问题标题】:Authorize with Controllers with AD Groups using OIDC使用 OIDC 向具有 AD 组的控制器授权
【发布时间】:2021-03-26 00:03:06
【问题描述】:

我们有一个 Web 应用程序,它使用 OIDC 进行单点登录以进行身份​​验证 天蓝色广告。单点登录效果很好,用户可以使用他们的 AD 帐户登录。它返回的令牌还包含 AD 组。 我想授权我的 MVC 控制器只允许某些组使用某些控制器。

我该如何实现呢?我可以看到这些组正以表示为 GUID 的令牌发回。 我尝试通过 RoleClaimType = "roles", 设置角色声明,但这不起作用。

这是我的代码。

  public void ConfigureAuth(IAppBuilder app)
    {
        FATContext db = new FATContext();

        

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies",
            CookieManager = new Microsoft.Owin.Host.SystemWeb.SystemWebChunkingCookieManager(),
        });

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = Authority,
                PostLogoutRedirectUri = postLogoutRedirectUri,
                TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = false,
                    // Set claimsPrincipal's roles to the roles claim
                    RoleClaimType = "roles",
                },
               
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {

                    RedirectToIdentityProvider = (context) =>
                    {
                       
                        context.ProtocolMessage.RedirectUri = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path);
                        context.ProtocolMessage.PostLogoutRedirectUri = new UrlHelper(HttpContext.Current.Request.RequestContext).Action("Index", "Home", null, HttpContext.Current.Request.Url.Scheme);

                        return Task.FromResult(0);
                    },
                    // If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
                    AuthorizationCodeReceived = (context) =>
                    {
                        
                        var code = context.Code;
                        ClientCredential credential = new ClientCredential(clientId, appKey);
                        string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                        AuthenticationContext authContext = new AuthenticationContext(Authority, new ADALTokenCache(signedInUserID));
                        return authContext.AcquireTokenByAuthorizationCodeAsync(
                           code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
                    }
                }
            });
      


    }

例如,我怎样才能让它与我的控制器一起工作,例如

 [AuthorizeUser(Roles = "xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
    public ActionResult Index()
    {
        return View();
    }

或检查用户是否处于某个角色,即 User.IsInRole("example ")

【问题讨论】:

    标签: asp.net-mvc azure-active-directory openid-connect


    【解决方案1】:

    在 Azure AD 中,我们可以定义应用程序角色并将角色分配给组。 现在该组的用户将拥有一个定义了角色的声明(示例如下所示)。有关如何在 Azure AD 中为应用程序创建/管理角色的详细文档,请参阅此 link

    {
      "roles": ["admin"]
    }
    

    如果您只是尝试使用基于组的授权,您可以在控制器或操作方法中使用 RoleClaimType = "groups" 和以下示例代码。更多详情你有类似信息here

    [Authorize(Roles = "Admin")]
    public class TestAdminAccessController : ApiController
    {
        public ActionResult Index()
        {
            if (this.User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Admin"))
                {
                    // Your logic
                }
                else
                {
                    // is NOT an admin. No permissions
                }
            }
            else
            {
                // is NOT Authenticated;    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多