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
。但是,在我們嘗試將用戶重定向到正確的登錄頁面後,該請求不再進行身份驗證,並且它們陷入無限循環。
我們可以做些什麼來防止這種情況發生?