2016-04-11 13 views
1

デバッガでUserオブジェクトを調べると、現在のメンバのUserDataプロパティである((System.Web.Security.FormsIdentity(User.Identity)).Ticket.UserDataに "admin"が表示されます。FormsAuthentication Ticketが設定されている場合、なぜUser.IsInRole( "Admin")は機能しませんか?

User.Identity.IsAuthenticatedが有効ですが、User.IsInRole("admin")はfalseを返します。

"admin"がUserDataプロパティにある場合、なぜUser.IsInRole( "admin")がtrueを返さないのですか?私のように設定した認証チケットを持っている私のログイン方法で

は次のとおりです。あなたがこれを見ることができるより多くの情報のためのあなたのGlobal.asax

protected void Application_AuthenticateRequest(Object sender, 
EventArgs e) 
{ 
    if (HttpContext.Current.User != null) 
    { 
    if (HttpContext.Current.User.Identity.IsAuthenticated) 
    { 
    if (HttpContext.Current.User.Identity is FormsIdentity) 
    { 
     FormsIdentity id = 
      (FormsIdentity)HttpContext.Current.User.Identity; 
     FormsAuthenticationTicket ticket = id.Ticket; 

     // Get the stored user-data, in this case, our roles 
     string userData = ticket.UserData; 
     string[] roles = userData.Split(','); 
     HttpContext.Current.User = new GenericPrincipal(id, roles); 
    } 
    } 
    } 
} 

でこのコードを配置する必要があり

FormsAuthenticationTicket _ticket = new FormsAuthenticationTicket(1, lUserName.Text, DateTime.Now, DateTime.Now.AddMonths(1), chk_remember.Checked, Role, FormsAuthentication.FormsCookiePath); 
     string encTicket = FormsAuthentication.Encrypt(_ticket); 
     HttpCookie _cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket); 
     if (chk_remember.Checked) 
      _cookie.Expires = DateTime.Now.AddMonths(1); 
     Response.Cookies.Add(_cookie); 
+0

誰でも手伝いできますか? –

答えて

2
+0

私の頭を2日間叩いた後、やっと正しい答えを得ました。本当にありがとう! –

関連する問題