用于Azure Active Directory和单一登录的访问令牌

时间:2018-07-24 14:51:31

标签: asp.net-mvc azure-active-directory access-token azure-ad-graph-api

我已经通过stackoverflow OpenID Azure access token上的此链接,也已经在GitHub上浏览了多个示例。让我解释一下情况。

我有一个具有专用登录名的ASP.NET MVC Web应用程序,但是客户端要求我与Azure Active Directory混合,因此我可以通过在Start_up.cs文件中添加代码来实现此目的(根本没有验证security_token和授权令牌。我需要吗?)

public void ConfigureAuth(IAppBuilder app)
{         
        string clientId = ConfigurationManager.AppSettings["ida:ClientID"];
        //string appKey = ConfigurationManager.AppSettings["ida:Password"];
        string tenantid = ConfigurationManager.AppSettings["tenantid"];
        //string graphResourceID = "https://graph.windows.net";
        string redirectUrl = ConfigurationManager.AppSettings["redirectUrl"];
        //fixed address for multitenant apps in the public cloud        
        string authority = String.Format(System.Globalization.CultureInfo.InvariantCulture, ConfigurationManager.AppSettings["Authority"], tenantid);

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions {
            CookieSecure = CookieSecureOption.Always,
        });

        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
                ClientId = clientId,
                Authority = authority,
                RedirectUri = redirectUrl,
                PostLogoutRedirectUri = redirectUrl,
                Scope = OpenIdConnectScopes.OpenIdProfile,
                ResponseType = OpenIdConnectResponseType.CodeIdToken,
                TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters
                {

                    ValidateIssuer = false,
                },
                Notifications = new OpenIdConnectAuthenticationNotifications()
                {

                    AuthenticationFailed = (context) =>
                    {
                        context.OwinContext.Response.Redirect("/Home/Error");
                        context.HandleResponse(); // Suppress the exception
                        return Task.FromResult(0);
                    }
                }
            });
}

我确实使用

[Authorize] 

在我的控制器中确保请求已通过身份验证,并重定向到正确的View。

但是,我的客户明确要求启用基于access_token的访问。我正尝试使用Graph API教程。

我是否要使用Graph API并在下面添加此类代码(仅供参考)来获取access_token的正确方法?

 AuthorizationCodeReceived = async (context) =>
   {
      var code = context.Code;

      ClientCredential credential = new ClientCredential(clientId, appKey);
      string tenantID = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value;
      string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;


      AuthenticationContext authContext = new AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}", tenantID), new EFADALTokenCache(signedInUserID));
      AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
                            code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceID);
      if(result!=null)
      {
         Console.WriteLine("access code received");
      }
  },
  SecurityTokenValidated = (context) =>
  {
     return Task.FromResult(0);
  }

我是否需要使用任何其他方式来获取access_token(我看到了很多使用grant_type等的示例,但这会使我更加困惑) access_token真的适合这种情况吗?

注意:这听起来可能是一个愚蠢的问题,但是现在是时候了,我必须清除这种困惑。

1 个答案:

答案 0 :(得分:2)

在OpenID Connect中,用户已通过身份验证,并且可以通过基于cookie的身份验证进行SSO。身份验证完成并将代表用户的令牌发送到您的应用程序后,OWIN中间件将创建会话cookie。然后,浏览器会在后续请求中使用此Cookie,因此用户无需重新输入密码,也不需要其他验证。

从您的描述看来,您似乎正在启用基于令牌的身份验证,并且应该满足客户的要求。

请参考此存储库以获取其他文档:https://github.com/Azure-Samples/active-directory-dotnet-webapp-openidconnect