如何在由WAAD保护的web api 2中获取电子邮件

时间:2017-10-23 17:37:03

标签: api azure owin azure-active-directory owin-middleware

我正在使用受“UseOpenIdConnectAuthentication”保护并获取Claims对象中所有用户详细信息的MVC 5客户端,此客户端通过“Bearer”身份验证令牌调用WAAD安全Web Api。

我需要在网络API中提取用户名或电子邮件。我尝试了不同的选择但没有任何效果。

我在Identity.Name中获取null,我得到的其他属性如nameidentifier,objectidentifier,tenanted等。

请告知。

由于 See the Identities I am getting

我正在使用Web Client中的访问令牌代码。

string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
            string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
            ClientCredential clientcred = new ClientCredential(Startup.clientId, Startup.appKey);
            AuthenticationContext authenticationContext = new AuthenticationContext(Startup.aadInstance + Startup.tenantId, new ADALTokenCache(signedInUserID));
            AuthenticationResult authenticationResult = authenticationContext.AcquireToken(apiResourceId, clientcred);

            return authenticationResult.AccessToken;

启动代码

app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ClientId = clientId,
                    Authority = Authority,
                    PostLogoutRedirectUri = postLogoutRedirectUri,
                    UseTokenLifetime = false,
                    Notifications = new OpenIdConnectAuthenticationNotifications()
                    {
                        // 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));
                            AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
                            code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
                            return Task.FromResult(0);

                        }

以下是令牌详细信息:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式获取当前用户:

var upn = ClaimsPrincipal.Current.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn").Value;

其他方式是使用Microsoft Graph api获取用户的基本信息,请参阅On-Behalf-Of scenario。OAuth 2.0 On-Behalf-Of流服务于应用程序调用服务/ Web API的用例需要调用另一个服务/ Web API。请参阅protocol explanationcode sample

更新:

查看您的代码,您正在使用client credential flow为您的网络API获取令牌:

AuthenticationResult authenticationResult = authenticationContext.AcquireToken(apiResourceId, clientcred);

OAuth 2.0客户端凭据授予流程允许Web服务(机密客户端)使用自己的凭据而不是模拟用户,以便在调用其他Web服务时进行身份验证。这就是为什么你无法获得与用户相关的信息。

您可以将authorization code flow与用户的身份一起使用,请参阅code sample

string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
ClientCredential credential = new ClientCredential(clientId, appKey);
result = await authContext.AcquireTokenSilentAsync(todoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));