我在我的應用程序中有一個安全管理器,可以同時適用於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
}
預先感謝任何幫助。
這是*答案。 – gahooa 2011-01-04 17:52:11