2010-05-17 33 views
1

我在我的應用程序中有一個安全管理器,可以同時適用於Windows和Web,過程很簡單,只需要用戶和密碼並對數據庫進行身份驗證,然後設置線程.CurrentPrincipal與自定義主體。對於Windows應用程序,這工作得很好,但我有Web應用程序的問題。從Thread.CurrentPrincipal設置HttpContext.Current.User

認證過程後,當我試圖從Thread.CurrentPrincipal設置Current.User到自定義主體時,最後一個包含GenericPrincipal。難道我做錯了什麼?這是我的代碼:

的Login.aspx

protected void btnAuthenticate_Click(object sender, EventArgs e) 
{ 
    SecurityManager.Authenticate("user","pwd"); // This is where I set the custom principal in Thread.CurrentPrincipal 
    FormsAuthenticationTicket authenticationTicket = new FormsAuthenticationTicket(1, 
         "user", 
         DateTime.Now, 
         DateTime.Now.AddMinutes(30), 
         false, 
         ""); 

    string ticket = FormsAuthentication.Encrypt(authenticationTicket); 
    HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticket); 
    Response.Cookies.Add(authenticationCookie);  
    Response.Redirect(FormsAuthentication.GetRedirectUrl("user", false)); 
} 

Global.asax中(這是問題出現在哪裏)

protected void Application_AuthenticateRequest(object sender, EventArgs e) 
{ 
    HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName]; 
    if (authCookie == null) 
     return; 

    if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.Identity is FormsIdentity) 
    {     
     HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal; //Here the value is GenericPrincipal 
    } 

預先感謝任何幫助。

回答

2

首先的長度:您需要根據每個請求設置委託人。

表單身份驗證使用它自己在每個請求上分配主體。這就是爲什麼你看到GenericPrincipal

我通常重新分配我的自定義prinicpal在OnPostAuthenticate標準身份驗證機制完成後。

+0

這是*答案。 – gahooa 2011-01-04 17:52:11

0

智能感知告訴我,HttpContext.Current.userSystem.Threading.Thread.CurrentPrincipal都是System.Security.Principal.IPrincipal類型。

如果我理解正確,這意味着HttpContext.Current.User = System.Threading.Thread.CurrentPrincipal;語句將在沒有任何轉換或轉換的情況下將線程主體的值分配給httpcontext用戶。

我沒有看到代碼中的確切位置,您正在設置System.Threading.Thread.CurrentPrincipal的值,但我建議您仔細檢查您如何設置委託人。我建議你在這段代碼上放置斷點並在調試器中運行它。查看正在發生的事情。

+0

我在登錄頁面的第一行設置了Thread.CurrentPrincipal,這是對用戶進行身份驗證並設置自定義主體的過程,這在Windows環境中工作正常,但問題出現在Web中。 – Argons 2010-05-17 15:11:01

-2

當您使用基於窗體的身份驗證(FBA)時,您將獲得ASP.NET中的GenericPrincipal。這很正常,你沒有做錯任何事。

你期待什麼?如果您期待WindowsPrincipal,則可以在ASP.NET應用程序中使用Windows集成身份驗證。

+0

嗯,我認爲你沒有隱瞞這個問題,GenericPrincipal可以改變爲自定義主體,我的答案是爲什麼當我把自定義主體放到應用程序的一層中的thread.CurrentPrincipal時,我無法訪問它在Web應用程序中。在問題的任何部分我都提到了WindowsPrincipal。 – Argons 2010-05-19 18:56:40

0

關於VB(orrible語言)對不起然而,這應該回答你的問題......

http://www.asp.net/security/videos/use-custom-principal-objects

然後改變該行的代碼,類似...

HttpContext.Current.User = (CustomPrinciple)System.Threading.Thread.CurrentPrincipal; 

那應該給你你需要的東西。 完美的作品對我來說:)

+0

如果你不喜歡VB.NET,有很多簡單的轉換工具可用,比如這個:http://converter.telerik.com/ – 2010-05-20 16:05:05

1

JFYI,可以更換

if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated && HttpContext.Current.User.Identity is FormsIdentity) { } 

FormsIdentity formsIdentity = HttpContext.Current.User as FormsIdentity; 
if (formsIdentity != null & formsIdentity.IsAuthenticated) { } 

減少if子句

相關問題