`[Authorize(Roles = “admin”)]` Infinite loop ASP.NET MVC and Azure Active Directory B2C

后端 未结 1 983
谎友^
谎友^ 2020-12-18 15:32

I am attempting to allow only Azure Active Directory B2C users with role \"Global Administrator\" to access the following class (which is why I have included the Autho

相关标签:
1条回答
  • 2020-12-18 16:08

    [Authorize(Roles = "admin")]

    Since your are using the Authorize attribute to check user's roles, you need to make sure the current user's Claims have the valid role claim. You could leverage the following code snippet to check your current user claims:

    return Json((User.Identity as ClaimsIdentity).Claims.Select(c => new { key = c.Type, value = c.Value }),JsonRequestBehavior.AllowGet);
    

    The problem is: when I click on the button that instantiates the UserProfileController, then the the code inside of AuthorizationCodeReceived = (context) => line of code is called again and again in an infinite loop.

    You could override the HandleUnauthorizedRequest method under AuthorizeAttribute and define your custom authorize attribute as follows:

    public class MyAuthorize : AuthorizeAttribute
    {
        protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
        {
            filterContext.Result = new ContentResult() { Content = "You don't have rights to take actions" };
        }
    }
    

    Then, you could decorate your UserProfileController controller as follows:

    [MyAuthorize(Roles = "admin")]
    public class UserProfileController : Controller
    {
        //TODO:
    }
    

    I am attempting to allow only Azure Active Directory B2C users with role "Global Administrator" to access the following class

    Under the AuthorizationCodeReceived delegate method, after you retrieve the access token, you need to leverage the Microsoft Graph Client library to check whether the current user is a Global Administrator / Company Administrator or not. If the current user is a Global Administrator / Company Administrator, then you need to specify the role claim as follows:

    context.AuthenticationTicket.Identity.AddClaim(new Claim(context.AuthenticationTicket.Identity.RoleClaimType, "admin"));
    

    Note: For checking whether a user is a Global Administrator, you could retrieve the roles under the current user's directory, then use the getMemberObjects API to retrieve the groups, roles that the current user is a member of, then check whether the Global Administrator role id is in current user's MemberObjects.

    //List directory roles, https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/directoryrole_list
    var roles=await graphClient.DirectoryRoles.Request().GetAsync();
    
    //user: getMemberObjects ,https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_getmemberobjects
    

    UPDATE:

    I checked the implementation on my side. Here is the code to check the role for the current logged user.

    var directoryRoles = await graphClient.DirectoryRoles.Request().GetAsync();
    var userRoles = await graphClient.Me.MemberOf.Request().GetAsync();
    
    var adminRole=directoryRoles.Where(role => role.DisplayName== "Company Administrator" || role.DisplayName == "Global Administrator").FirstOrDefault();
    if (userRoles.Count(role => role.Id == adminRole.Id) > 0)
    {
        context.AuthenticationTicket.Identity.AddClaim(new Claim(context.AuthenticationTicket.Identity.RoleClaimType, "admin"));
    }
    else
    {
        context.AuthenticationTicket.Identity.AddClaim(new Claim(context.AuthenticationTicket.Identity.RoleClaimType, "user"));
    }
    

    Note: For adding multiple user roles, you could add multiple new Claim(context.AuthenticationTicket.Identity.RoleClaimType, "<role-name>") role claims.

    Here is my modified custom AuthorizeAttribute:

    public class MyAuthorize : AuthorizeAttribute
    {
        private bool noPermission = false;
    
        public string Permissions { get; set; }
    
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (!base.AuthorizeCore(httpContext))
                return false;
    
            var permissionArrs = Permissions.Trim().Split('|');
    
            if (permissionArrs.ToList().Exists(p=>httpContext.User.IsInRole(p)))
            {
                return true;
            }
            else
            {
                noPermission = true;
                return false;
            }
        }
    
        protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
        {
            if (noPermission)
                filterContext.Result = new ContentResult() { Content = "You don't have rights to take actions" };
            else
                base.HandleUnauthorizedRequest(filterContext);
        }
    }
    

    Decorate the UserProfileController as follows:

    [MyAuthorize(Permissions = "admin|co-admin")]
    public class UsersController : Controller
    {
       //TODO:
    }
    
    0 讨论(0)
提交回复
热议问题