代码之家  ›  专栏  ›  技术社区  ›  Unnie

将应用程序角色作为自定义声明添加到Azure广告标识

  •  0
  • Unnie  · 技术社区  · 7 年前

    我有一个webapp,它支持基于表单的身份验证(使用OOTB Asp.net identity framework)和基于Azure广告的身份验证(使用OpenID Connect)。身份验证对基于表单的用户和Azure AD用户都很有效。现在,这些角色在数据库中(使用OOTB Asp.net identity framework),这些角色作为所有Asp.net的声明获得。net身份用户。但对于Azure广告标识用户,我无法获得角色声明。我尝试通过电子邮件查询数据库,获取Azure AD用户的角色,然后将其设置为角色声明,如下所示:

    User.SetClaims(ClaimTypes.Role,roleAdministrator.Name);//calling the set method
    

    将声明设置如下。

     public static void SetClaims(this IPrincipal user,string claimType,string claimValue)
            {
                if (user?.Identity is ClaimsIdentity claimsIdentity)
                {
                    var name = claimsIdentity.FindFirst(claimType);
                    if (name != null)
                        claimsIdentity.RemoveClaim(name);
                    claimsIdentity.AddClaim(new Claim(claimType, claimValue));
                }
            }
    

    这只适用于特定的http请求,只要我单击其他地方或刷新页面,角色声明就不再存在于Azure广告标识中。

    如何使用asp管理的角色实现Azure AD auth。网络身份角色(而不是Azure广告角色)

    请注意,使用Azure广告角色目前不是我的选择,因为这是一个带有asp的现有应用程序。net身份和角色。我正在尝试以最小的更改再添加一个身份验证提供程序。因此,角色必须像现在一样存在于数据库中。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Tom Sun    7 年前

    如果我们想为Azure AD用户添加自定义属性,我们可以使用 Directory schema extensions ,可用于向目录对象添加属性,而无需外部数据存储。你可以跟着这个 tutorial 要注册扩展,请写入和读取扩展值。

    如果Azure广告B2C是可能的,你可以 define custom attributes in Azure Active Directory B2C .

    有关如何获取自定义属性,请参阅 another SO thread .