Azure active directory 如何通过Azure广告检查用户是否在广告组中?

Azure active directory 如何通过Azure广告检查用户是否在广告组中?,azure-active-directory,microsoft-graph-api,Azure Active Directory,Microsoft Graph Api,设置规范 .NET 4.5.1 MVC项目 项目包含.aspx文件(旧版) 当前用户Azure AD通过Cookie进行身份验证 Azure门户(通过应用程序注册)配置了“隐式授权ID令牌”和“仅此组织目录中的帐户” 本地广告组被推到Azure广告 Startup.cs配置 // COOKIES: Tells it to use cookies for authentication. app.SetDefaultSignInAsAuthenticationType(CookieAuthent

设置规范

  • .NET 4.5.1 MVC项目
  • 项目包含.aspx文件(旧版)
  • 当前用户Azure AD通过Cookie进行身份验证
  • Azure门户(通过应用程序注册)配置了“隐式授权ID令牌”和“仅此组织目录中的帐户”
  • 本地广告组被推到Azure广告
Startup.cs配置

// COOKIES: Tells it to use cookies for authentication.
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
    CookieManager = new SystemWebCookieManager()
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
    ClientId = ClientID,
    Authority = Authority,
    PostLogoutRedirectUri = PostLogoutRedirectUri,
    Notifications = new OpenIdConnectAuthenticationNotifications()
    {
        AuthenticationFailed = PrincipalService.OnAzureAuthenticationFailure,
        AuthorizationCodeReceived = (AuthorizationCodeReceivedNotification notification) =>
        {
            var username = notification.AuthenticationTicket.Identity.Name.Split('#').LastOrDefault();
            var emailAddress = notification.AuthenticationTicket.Identity.Claims.FirstOrDefault(x => x.Type.Contains("emailaddress"))?.Value;
            Logger.Log(Level.Auth, $"Azure login success! Username: '{username}' Email: '{emailAddress}'.");
            return Task.FromResult(0);
        }
    }
});
问题

// COOKIES: Tells it to use cookies for authentication.
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
    CookieManager = new SystemWebCookieManager()
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
    ClientId = ClientID,
    Authority = Authority,
    PostLogoutRedirectUri = PostLogoutRedirectUri,
    Notifications = new OpenIdConnectAuthenticationNotifications()
    {
        AuthenticationFailed = PrincipalService.OnAzureAuthenticationFailure,
        AuthorizationCodeReceived = (AuthorizationCodeReceivedNotification notification) =>
        {
            var username = notification.AuthenticationTicket.Identity.Name.Split('#').LastOrDefault();
            var emailAddress = notification.AuthenticationTicket.Identity.Claims.FirstOrDefault(x => x.Type.Contains("emailaddress"))?.Value;
            Logger.Log(Level.Auth, $"Azure login success! Username: '{username}' Email: '{emailAddress}'.");
            return Task.FromResult(0);
        }
    }
});
鉴于此设置,我如何检查当前登录的用户是否在特定的广告组中

我尝试过的

关于使用Microsoft Graph API的所有指南都会遇到一个我不知道如何克服的问题(例如,
GetAccountsAsync
返回空,等等)

我将以下内容添加到我们的应用程序注册清单中:

"optionalClaims": {
    "idToken": [
        {
            "name": "email",
            "source": null,
            "essential": true,
            "additionalProperties": []
        },
        {
            "name": "groups",
            "source": null,
            "essential": true,
            "additionalProperties": []
        }
    ],
    "accessToken": [],
    "saml2Token": []
}

电子邮件
工作正常,但显然
小组
工作不正常,因为这是一个暗箱操作。

1。作为令牌的一部分获取组成员资格声明

// COOKIES: Tells it to use cookies for authentication.
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
    CookieManager = new SystemWebCookieManager()
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
    ClientId = ClientID,
    Authority = Authority,
    PostLogoutRedirectUri = PostLogoutRedirectUri,
    Notifications = new OpenIdConnectAuthenticationNotifications()
    {
        AuthenticationFailed = PrincipalService.OnAzureAuthenticationFailure,
        AuthorizationCodeReceived = (AuthorizationCodeReceivedNotification notification) =>
        {
            var username = notification.AuthenticationTicket.Identity.Name.Split('#').LastOrDefault();
            var emailAddress = notification.AuthenticationTicket.Identity.Claims.FirstOrDefault(x => x.Type.Contains("emailaddress"))?.Value;
            Logger.Log(Level.Auth, $"Azure login success! Username: '{username}' Email: '{emailAddress}'.");
            return Task.FromResult(0);
        }
    }
});
通过编辑应用程序的清单(可直接在Azure Portal中完成),并根据需要将
“groupMembershipClaims”
属性设置为
“All”
“SecurityGroup”
,可以使组声明成为应用程序访问令牌的一部分

2。组ID作为索赔的一部分返回

一旦应用程序清单如上所述更新,您就可以将组Id作为声明的一部分。下面是一个解码的JWT令牌的快速示例

3。可作为令牌一部分返回的组数限制

// COOKIES: Tells it to use cookies for authentication.
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
    CookieManager = new SystemWebCookieManager()
});

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions()
{
    ClientId = ClientID,
    Authority = Authority,
    PostLogoutRedirectUri = PostLogoutRedirectUri,
    Notifications = new OpenIdConnectAuthenticationNotifications()
    {
        AuthenticationFailed = PrincipalService.OnAzureAuthenticationFailure,
        AuthorizationCodeReceived = (AuthorizationCodeReceivedNotification notification) =>
        {
            var username = notification.AuthenticationTicket.Identity.Name.Split('#').LastOrDefault();
            var emailAddress = notification.AuthenticationTicket.Identity.Claims.FirstOrDefault(x => x.Type.Contains("emailaddress"))?.Value;
            Logger.Log(Level.Auth, $"Azure login success! Username: '{username}' Email: '{emailAddress}'.");
            return Task.FromResult(0);
        }
    }
});
为了确保令牌大小不超过HTTP头大小限制,Azure AD限制了它在组声明中包含的ObjectID的数量。如果用户是超过超龄限制的组的成员(SAML令牌为150,JWT令牌为200),则Azure AD不会在令牌中发出组声明。相反,它在令牌中包含一个overage声明,指示应用程序查询Graph API以检索用户的组成员资格

4。相关的Microsoft Graph API

注意:使用Microsoft Graph API可能非常强大,因为您可以绕过超龄场景,并在需要时获取有关组的所有其他类型的信息(如名称)。在这种特殊情况下,由于目的是验证组成员身份,所以组Id是最好的字段,因为它不会更改,而像name这样的其他字段可以更改

如果您已经知道要检查/验证其成员资格的组,则此选项将非常有用

 POST https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/checkMemberGroups 
在请求正文中,您可以提供
groupdIds
,即包含要检查其成员资格的组的对象ID的集合。最多可指定20组

     {
      "groupIds": [
           "fee2c45b-915a-4a64b130f4eb9e75525e",
           "4fe90ae065a-478b9400e0a0e1cbd540"
       ]
     }

如果您还不了解该组,并且希望获取该用户所属的所有组,则此选项将非常有用

POST https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/getMemberGroups

这里是另一个

您还可以通过编辑应用程序的清单(这可以直接在Azure Portal中完成)并根据需要将
“groupMembershipClaims”
属性设置为
“All”
“SecurityGroup”
,使组声明成为应用程序访问令牌的一部分。要知道,当用户所属的组数较多时。。您可能无法将所有组作为令牌的一部分,但可以使用超龄指示符。。在这种情况下,您可能需要单独查询组,好的,所以我将“groupMembershipClaims”设置为“All”,并返回GUID。有没有办法让它返回组的名称?请看这篇文章,从Microsoft graph的角度了解更详细的解释和其他一些选项。。一点也不。。作为索赔的一部分,您只能获得组ID。。不是组名。。如果您在查找组名,Microsoft Graph API可能是您的最佳选择(因为这也有助于解决任何超龄情况)。如果您希望验证用户是否属于组,则无论如何都应该使用组Id。。名字总是可以改变的。。我将回答您的问题
在这种设置下,我如何检查当前登录的用户是否在特定的广告组中?
。。另外,在使用Microsoft Graph API时,我不太了解您的问题。。如果你能详细说明的话,也许我或其他人能帮上忙