2012-01-24 20 views
4

に動作していない:私はそのメニューで、メニューを持っているマスターページでがHttpContext.Current.User.IsInRole私はこのコードを持っている私のコントローラAuthController /サインインで

entities.UserAccount user = (new BLL.GestionUserAccount()).authentifier(email, password); 
      //storing the userId in a cookie 
      string roles = (new BLL.GestionUserAccount()).GetUserRoles(user.IdUser); 
      // Initialize FormsAuthentication, for what it's worth 

      FormsAuthentication.Initialize(); 

      // 

      FormsAuthentication.SetAuthCookie(user.IdUser.ToString(), false); 

      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
      1, // Ticket version 
      user.IdUser.ToString(), // Username associated with ticket 
      DateTime.Now, // Date/time issued 
      DateTime.Now.AddMinutes(30), // Date/time to expire 
      true, // "true" for a persistent user cookie 
      roles, // User-data, in this case the roles 
      FormsAuthentication.FormsCookiePath);// Path cookie valid for 

      // Encrypt the cookie using the machine key for secure transport 
      string hash = FormsAuthentication.Encrypt(ticket); 
      HttpCookie cookie = new HttpCookie(
       FormsAuthentication.FormsCookieName, // Name of auth cookie 
       hash); // Hashed ticket 



       // Get the stored user-data, in this case, our roles 

      // Set the cookie's expiration time to the tickets expiration time 
      if (ticket.IsPersistent) cookie.Expires = ticket.Expiration; 

      // Add the cookie to the list for outgoing response 
      Response.Cookies.Add(cookie); 
      return RedirectToAction("index", "Home"); 

をすることを意図されている項目があります管理者役割によってのみ表示されます。

 <% if (HttpContext.Current.User.IsInRole("admin")){ %> 

      <%=Html.ActionLink("Places", "Places", "Places")%> 
     <%} %> 

でもHttpContext.Current.Userは、右の役割をconatiningで、私はアイテムを見ることができない。

enter image description here

globalxの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); 
       } 
      } 
     } 
    } 
+0

'Current.User'ロールプリンシパルが正しく作成されているかどうか確認できますか? – gideon

+0

gideo、image.myの役割を確認してください。 –

+0

私はそれが愚かに聞こえるが、あなたのチケットからあなたの 'userData'を知っている。 'userData'がプリンシパルに入っていない場合、私が考えることができるのは唯一のことです。 (おそらく 'glabal.asax'の最後の3行に問題があります)ちょっと考えました。 – gideon

答えて

1

私はそれが馬鹿に聞こえることは知っていますが、あなたの画像から私はあなたのチケットからあなたのuserDataを見ることができます。

userDataがプリンシパルに入らない場合は、私が考えることができる唯一のものです。 (glabal.asax.csの最後の3行でおそらく問題)

何かが間違ってここにある:

string userData = ticket.UserData; 
string[] roles = userData.Split(','); 
HttpContext.Current.User = new GenericPrincipal(id, roles); 
1

あなたが必要となります認証チケットのユーザーデータ部分を解析し、IPrincipalを手動で作成するカスタムAuthorize属性。 this postを見てください。これは、ASP.NET MVCでこれを行うことをお勧めします。 ASP.NET MVCアプリケーションでHttpContext.Currentを使用しないでください。あなたの意見でさえも。代わりに<% if (User.IsInRole("admin")) { %>を使用してください。

+0

ダーリン私は更新しました。 –

+1

@ user594166、私はASP.NET MVCアプリケーションを進めることをどのように推薦するかの例へのリンクを使って自分の答えを更新しました:http://stackoverflow.com/a/5476355/29407これはMVCishのやり方ですこれを達成する。 –

+0

どのようにMVCishですか? MyAuthorizeAttribute:AuthorizeAttribute? –

5

代わりUser.IsInRole()を使用するのでは、静的メソッドRoles.IsUserInRole()を試してみてください。

0

1つのステートメントがありません。この行の後

あなたはこの行を配置する必要があり
FormsAuthenticationTicket ticket = id.Ticket; 

private static void SetPrincipal(IPrincipal principal) 
    { 
     Thread.CurrentPrincipal = principal; 
     if (HttpContext.Current != null) 
     { 
      HttpContext.Current.User = principal; 
     } 
    } 

私が見つけた:そのような2つのオブジェクトのGlobal.asaxのアサインプリンシパルで

ticket = FormsAuthentication.Decrypt(ticket.Name); 
関連する問題