2012-04-20 12 views
2

ビルド中のサイトに完全にログインする前に、特定のユーザーに役割があるかどうかを確認しようとしています。通常、私は、コードを使用します。UserNameの文字列が与えられている場合、そのユーザーがIsInRoleかどうかをどのように確認できますか?

User.IsInRole("CustomRole") 

をしかし、常に「false」に生成ラインこのような状況で - 私はUser.IsInRoleを使用するために、ユーザがすでに完全にログインする必要があるため、これがあると信じて私がしようとしています。私のアカウントコントローラのLogOnメソッド内でこの情報をチェックすると、ユーザーはまだログインしていない(私は思う)。

'System.Web.Security.MembershipUser' does not contain a definition for 'IsInRole' and no extension method 'IsInRole' accepting a first argument of type 'System.Web.Security.MembershipUser' could be found (are you missing a using directive or an assembly reference?) 

たMembershipUser U =メンバーシップ:

public ActionResult LogOn(LogOnModel model, string returnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     if (Membership.ValidateUser(model.UserName, model.Password)) 
     { 
      FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 

      // Need to identify the user because the "User" is not officially 'logged in' yet and cannot be accessed via "User.IsInRole" - am I correct in this understanding 
      MembershipUser u = Membership.GetUser(model.UserName); 
      if (u.IsInRole("Administrator")) 

. . . . truncated 

上記のコードは次のようなエラーがスローされます。私は以下を実行しようとしています何を行うことができますように、私はユーザーオブジェクトを返す方法

.GetUser(model.UserName)は、明らかに、私はIsInRoleを使うことができるオブジェクトを返していません。

答えて

5

var authorized = Roles.IsUserInRole(username, roleName); 
です
0

ロールとユーザー名の両方を必要とするオーバーロードがあると思います。何をしたいMSDN

関連する問題