2015-04-03 26 views
1

我們有一個移動應用程序,想要訪問我們的ASP.NET MVC4網站上的視圖。爲此,應用程序需要通過與我們的ADFS登錄過程不同的方式進行身份驗證。移動應用向該服務發出請求,並將用戶名和密碼作爲請求標頭傳入。在ASP.NET應用程序global.asax文件,我們有以下幾點:如何通過將登錄信息作爲請求標頭傳遞來針對ADFS進行身份驗證?

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
    { 
     string username = HttpContext.Current.Request.Headers[Mobile.Configuration.iPadUsername]; 
     string password = HttpContext.Current.Request.Headers[Mobile.Configuration.iPadPassword]; 
     string acctID = HttpContext.Current.Request.Headers[Mobile.Configuration.iPadAcctStr]; 

     //bypass adfs 
     if (!string.IsNullOrEmpty(username) 
      && !string.IsNullOrEmpty(password) 
      && !HttpContext.Current.Request.IsAuthenticated) 
     { 

      //web service call to authenticate the user 

      if (success) 
      { 
       var genIden = new GenericIdentity(usrnm); 
       var genClaim = new GenericPrincipal(genIden, new string[] { }); 
       HttpContext.Current.User = genClaim; 
       var token = FederatedAuthentication.SessionAuthenticationModule.CreateSessionSecurityToken(genClaim, "test mobile", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true); 
       FederatedAuthentication.SessionAuthenticationModule.AuthenticateSessionSecurityToken(token, true); 


       Response.Clear(); 

       Response.Status = "301 Moved Permanently"; 

       Response.AddHeader("Location", "/Default/IndexRedirectFromMobile"); 

       Response.End(); 
      } 
     } 
    } 

結束意外事件發生時設置的用戶上面的GeneralPrincipal的時候,我們有HttpContext.Current.Request.IsAuthenticated設置爲true。但是,在我們嘗試將用戶重定向到正確的登錄頁面後,該請求不再進行身份驗證,並且它們陷入無限循環。

我們可以做些什麼來防止這種情況發生?

回答

1

最簡單的方法是將令牌寫入到一個cookie

var token = FederatedAuthentication.SessionAuthenticationModule.CreateSessionSecurityToken(genClaim, "test mobile", DateTime.UtcNow, DateTime.UtcNow.AddDays(1), true); 

FederatedAuthentication.SessionAuthenticationModule.WriteSessonTokenToCookie(token); 

你需要記住的是,客戶端必須支持Cookie。根據您使用的實際客戶端技術,這可能更容易或更困難。

但是,您不需要使用SAM模塊來保留會話。您可以使用表單身份驗證,自定義身份驗證標頭或其他任何東西

相關問題