2

これはMVC 3プロジェクトです。テストのためだけに、私はRoleProviderを動作させるには?

public class MyRoleProvider : RoleProvider 
{ 
    public override string[] GetRolesForUser(string username) 
    { 
     return new string[] { "0", "1", "2", "4" }; 
    } 

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

私はweb.configに登録します。そして、標準のSqlMemberShipProviderを設定すると、次のようなものがGetRolesForUserを起動させます。

[Authorize(Roles="4")] 
public class AdminController : Controller 
{ //... 

ただし、標準のSqlMemberShipProviderは使用しません。次のように私はちょうどテストに、自分のAuthorizeAttributeによって定義された:

public class MyAuthorize : AuthorizeAttribute 
{ 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     bool result = true; 
     return result; 
     return base.AuthorizeCore(httpContext); 
    } 

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     base.HandleUnauthorizedRequest(filterContext); 
    } 

    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     base.OnAuthorization(filterContext); 
    } 
} 

さて、以下はもう発射するMyRoleProvider.GetRolesForUserが発生することはありません。

[MyAuthorize(Roles="4")] 
public class AdminController : Controller 
{ //... 

上記MyAuthorize.AuthorizeCoreとMyAuthorize.OnAuthorizationなくMyRoleProviderのメソッドをトリガします。 MemberShipProviderとRoleProviderとAuthorizedAttributeの関係は何ですか?これらの関係はいつ定義または構成されますか?

ありがとうございました。

答えて

2

からの抜粋です。私は通常それをコメントアウトするか削除します。

<roleManager defaultProvider="MyRoleProvider" enabled="true"> 
    <providers> 
    <clear /> 
    <!--<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> 
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />--> 
    <add name="MyRoleProvider" type="Full.Namespace.Of.MyRoleProvider" applicationName="/" /> 
    </providers> 
</roleManager> 
+0

元の投稿を削除します。 テスト中に間違いを犯したことがあります。 私はもう一度やり直して、MyAuthorize属性を使用していてもMyRoleProviderが呼び出されることを確認します。 回答したすべての方に感謝します –

0

それはタイプミスだ場合、私は知りませんが、それは常にそう

protected override bool AuthorizeCore(HttpContextBase httpContext) 
{ 
    bool result = true; 
    return result; 
    return base.AuthorizeCore(httpContext); 
} 

リターン真の役割にユーザーをチェックすると、基本メソッドを発射していないbase.AuthorizeCoreです。ここで

bool result = true; 
return result; 

を削除しようと、あなたは標準SqlRoleProviderを使用したくない場合は、それを構成しないMVCソース

// This method must be thread-safe since it is called by the thread-safe OnCacheAuthorization() method. 
    protected virtual bool AuthorizeCore(HttpContextBase httpContext) { 
     if (httpContext == null) { 
      throw new ArgumentNullException("httpContext"); 
     } 

     IPrincipal user = httpContext.User; 
     if (!user.Identity.IsAuthenticated) { 
      return false; 
     } 

     if (_usersSplit.Length > 0 && !_usersSplit.Contains(user.Identity.Name, StringComparer.OrdinalIgnoreCase)) { 
      return false; 
     } 

     if (_rolesSplit.Length > 0 && !_rolesSplit.Any(user.IsInRole)) { 
      return false; 
     } 

     return true; 
    } 
+0

ありがとう:

あなたの設定は次のようになります。タイプミスではありません。私はイベントの流れをトラップして、私が自分の認証コードにどこに置くことができるかを見ました(私はASP.NET MembershipProviderから独立して他の場所でうまく実装したGoogleアカウントを使用しようとしています)。 実際、私はMemberShipProviderとAuthorize属性の間の関係を把握していません。 AuthorizeCoreをオーバーライドしても標準のMembershipProviderを引き続き使用するとはどういう意味ですか? RoleProviderを使用する場合、AuthorizeCoreをトラップするにはどうすればよいですか? –

+0

これは誤字ではなく古いコードです。 –

関連する問題