2015-05-28 16 views
8

Tôi có một ứng dụng asp.net mvc với dấu AAD azure. Khi tôi nhấn f5 để gỡ lỗi ứng dụng đi đến azure để xác thực trong AAD, sau đó nó quay trở lại ứng dụng để điều khiển, và chuyển hướng của nó trở lại một lần nữa để azure.asp.net mvc azure AAD xác thực vòng lặp vô hạn

Tôi biết điều này vì Nếu tôi đặt một breakpoint trên Đăng nhập điều khiển nó được nhấn vô

Đây là tuyến đường của tôi cấu hình

public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      //routes.IgnoreRoute(""); 
      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional } 
      ); 
     } 

này được điều khiển bảng điều khiển của tôi trong đó có ủy quyền cho

[Authorize] 
    public class DashboardsController : Controller 
    { 
     public ActionResult Dashboard_1() 
     { 
      return View(); 
     } 

Đây là hành động đăng nhập và đăng nhập của tài khoản của tôi

public class AccountController : Controller 
    { 
     public void SignIn() 
     { 
      if (!Request.IsAuthenticated) 
      { 
       HttpContext.GetOwinContext().Authentication.Challenge(
           new AuthenticationProperties { RedirectUri = "/" }, 
           OpenIdConnectAuthenticationDefaults.AuthenticationType); 
      } 
     } 

     public void SignOut() 
     { 
      // Remove all cache entries for this user and send an OpenID Connect sign-out request. 
      string usrObjectId = ClaimsPrincipal.Current.FindFirst(SettingsHelper.ClaimTypeObjectIdentifier).Value; 
      AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, new EfAdalTokenCache(usrObjectId)); 
      authContext.TokenCache.Clear(); 

      HttpContext.GetOwinContext().Authentication.SignOut(
       OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType); 
     } 

     public ActionResult ConsentApp() 
     { 
      string strResource = Request.QueryString["resource"]; 
      string strRedirectController = Request.QueryString["redirect"]; 

      string authorizationRequest = String.Format(
       "{0}oauth2/authorize?response_type=code&client_id={1}&resource={2}&redirect_uri={3}", 
        Uri.EscapeDataString(SettingsHelper.AzureADAuthority), 
        Uri.EscapeDataString(SettingsHelper.ClientId), 
        Uri.EscapeDataString(strResource), 
        Uri.EscapeDataString(String.Format("{0}/{1}", this.Request.Url.GetLeftPart(UriPartial.Authority), strRedirectController)) 
        ); 

      return new RedirectResult(authorizationRequest); 
     } 

     public ActionResult AdminConsentApp() 
     { 
      string strResource = Request.QueryString["resource"]; 
      string strRedirectController = Request.QueryString["redirect"]; 

      string authorizationRequest = String.Format(
       "{0}oauth2/authorize?response_type=code&client_id={1}&resource={2}&redirect_uri={3}&prompt={4}", 
        Uri.EscapeDataString(SettingsHelper.AzureADAuthority), 
        Uri.EscapeDataString(SettingsHelper.ClientId), 
        Uri.EscapeDataString(strResource), 
        Uri.EscapeDataString(String.Format("{0}/{1}", this.Request.Url.GetLeftPart(UriPartial.Authority), strRedirectController)), 
        Uri.EscapeDataString("admin_consent") 
        ); 

      return new RedirectResult(authorizationRequest); 
     } 

     public void RefreshSession() 
     { 
      string strRedirectController = Request.QueryString["redirect"]; 

      HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = String.Format("/{0}", strRedirectController) }, OpenIdConnectAuthenticationDefaults.AuthenticationType); 
     } 
    } 

và đây là startup.auth.cs tôi

public void ConfigureAuth(IAppBuilder app) 
     { 
      // configure the authentication type & settings 
      app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 
      app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

      // configure the OWIN OpenId Connect options 
      app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions 
      { 
       ClientId = SettingsHelper.ClientId, 
       Authority = SettingsHelper.AzureADAuthority, 
       Notifications = new OpenIdConnectAuthenticationNotifications() 
       { 
        // when an auth code is received... 
        AuthorizationCodeReceived = (context) => { 
         // get the OpenID Connect code passed from Azure AD on successful auth 
         string code = context.Code; 

         // create the app credentials & get reference to the user 
         ClientCredential creds = new ClientCredential(SettingsHelper.ClientId, SettingsHelper.ClientSecret); 
         string userObjectId = context.AuthenticationTicket.Identity.FindFirst(System.IdentityModel.Claims.ClaimTypes.NameIdentifier).Value; 

         // use the ADAL to obtain access token & refresh token... 
         // save those in a persistent store... 
         EfAdalTokenCache sampleCache = new EfAdalTokenCache(userObjectId); 
         AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.AzureADAuthority, sampleCache); 

         // obtain access token for the AzureAD graph 
         Uri redirectUri = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)); 
         AuthenticationResult authResult = authContext.AcquireTokenByAuthorizationCode(code, redirectUri, creds, SettingsHelper.AzureAdGraphResourceId); 

         // successful auth 
         return Task.FromResult(0); 
        }, 
        AuthenticationFailed = (context) => { 
         context.HandleResponse(); 
         return Task.FromResult(0); 
        } 
       }, 
       TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters 
       { 
        ValidateIssuer = false 
       } 
      }); 
     } 
+0

Bạn có thấy các cookie .Spnet đang được thiết lập không? Ngoài ra, hãy xem điều này: https://katanaproject.codeplex.com/workitem/197, nó có thể hữu ích. – ezile

+0

Bạn đã giải quyết vấn đề này chưa? Có cùng một vấn đề? * bump * – Marcus

Trả lời

2

Chúng tôi chạy vào cùng một vấn đề và giải quyết nó bằng cách trượt trong cookie saver Kentor. Xem https://github.com/KentorIT/owin-cookie-saver để biết chi tiết.

+0

Cảm ơn bạn đã giải pháp. Chúng tôi đã sử dụng nó và nó hoạt động. – dlght

Các vấn đề liên quan