次の単純なシナリオでは、Roles.IsUserInRole は期待どおりに動作しますか?

StackOverflow https://stackoverflow.com/questions/362907

  •  21-08-2019
  •  | 
  •  

質問

.NET 2.0 のカスタム ロール プロバイダー (RoleProvider から継承) では、IsUserInRole メソッドが常に true を返すようにハードコードされています。

public override bool IsUserInRole(string username, string roleName) { return true; }

このロール プロバイダーを使用するように構成された ASP.NET アプリケーションでは、次のコードは (期待どおり) true を返します。

Roles.IsUserInRole("any username", "any rolename"); // results in true

ただし、次のコードは false を返します。

Roles.IsUserInRole("any rolename"); // results in false

User.IsInRole("any rolename") も false を返すことに注意してください。

  1. これは予期された動作ですか?
  2. ロール名のみを取るオーバーロードがオーバーライドされた IsUserInRole を引き続き呼び出すと考えるのは間違っていますか?

アップデート:単一の文字列を受け取るバージョンでは利用可能なオーバーライドがないようであることに注意してください。これが #2 での私の推測につながりました。

役に立ちましたか?

解決

私は、.NETリフレクタでRoles.IsUserInRole(文字列ロール名)を見て、それは以下に解決

public static bool IsUserInRole(string roleName)
{
    return IsUserInRole(GetCurrentUserName(), roleName);
}

私はあなたの現在のユーザーを見てみましょう。その理由は次のとおりです。

private static string GetCurrentUserName()
{
    IPrincipal currentUser = GetCurrentUser();
    if ((currentUser != null) && (currentUser.Identity != null))
    {
        return currentUser.Identity.Name;
    }
    return string.Empty;
}

あなたは現在のユーザーを持っていないのいずれか、またはその名前は空の文字列またはnullであるので、私は、これは空の文字列を返している賭けることをいとわない。

IsUserInRole(string username, string roleName)法では、次のコードブロックがある右の先頭近くます:

   if (username.Length < 1)
   {
       return false;
   }

あなたのGetCurrentUserName()が意味のあるものを返さない場合、それはあなたのオーバーライドされたメソッドを呼び出す前に、それはfalseを返します。

このから離れて取ることが教訓:リフレクターは素晴らしいツールは次のとおりです。)

他のヒント

あなたはRoleManagerの設定でcacheRolesInCookie =「true」を選択した場合にも注意してください。あなたは、データベースに新しいロールを追加した場合、それはクッキーでキャッシュされたバージョンを見ている可能性があります。

私はこの問題を抱えていたし、解決策は、クッキーと再ログインを削除することでした。

これは、誰かを助けるかもしれない - に注意してます:

あなたが認証するためにログインコントロールを使用している場合 - コントロールに入力されたユーザ名は、より具体的にRoles.IsUserInRole(文字列のロール名)で使用されHttpContext.Current.User.Identity.Nameなり - 会員のGETUSER( ) 方法。したがって、このような場合にはあなたは、認証イベントをオーバーライドし、この方法でユーザーを検証し、カスタムメンバシッププロバイダを使用することができる値にユーザー名を設定してください。

 protected void crtlLoginUserLogin_Authenticate(object sender, AuthenticateEventArgs e)
{
    bool blnAuthenticate = false;
    string strUserName = crtlLoginUserLogin.UserName;

    if (IsValidEmail(strUserName))
    {

        //if more than one user has email address - must authenticate by username.

        MembershipUserCollection users = Membership.FindUsersByEmail(strUserName);
        if (users.Count > 1)
        {
            crtlLoginUserLogin.FailureText = "We are unable to determine which account is registered to that email address. Please enter your Username to login.";

        }
        else
        {
            strUserName = Membership.GetUserNameByEmail(strUserName);
            blnAuthenticate = Membership.ValidateUser(strUserName, crtlLoginUserLogin.Password);

            //setting the userLogin to the correct user name (only on successful authentication)
            if (blnAuthenticate)
            {
                crtlLoginUserLogin.UserName = strUserName;
            }

        }


    }
    else
    {
        blnAuthenticate = Membership.ValidateUser(strUserName, crtlLoginUserLogin.Password);
    }

    e.Authenticated = blnAuthenticate;

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top