0

ASP.NET MVC 2 アプリケーションを作成していますが、ASP.NET メンバーシップを使用したくありません。コントローラーで Authorize 属性を使用したいと考えています。私がこれまでやってきたことは...

Web.config

<roleManager enabled="true" />

<authentication mode="Forms">
  <forms loginUrl="~/Authentication/Login" timeout="2880"/>
</authentication>
<authorization>
  <allow users="*" /> /* This is for testing */
</authorization>

私のGlobal.asaxで

 protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        var cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

        if (cookie == null) return;
        var decryptedCookie = FormsAuthentication.Decrypt(cookie.Value);            
        var roles = decryptedCookie.UserData.Split('|');

        var tcmIdentity = new TcmIdentity(decryptedCookie.Name);
        var tcmPrincipal = new GenericPrincipal(tcmIdentity, roles);

        Context.User = tcmPrincipal;
    }

将来、いくつかのカスタム プロパティを追加できるように、カスタム IIdentity を使用しています。これをコントローラーアクションでテストするために、これを行いました...

var testPrincipal = User;

すべてのユーザー情報を含むカスタム ID を確認できますが、プリンシパル オブジェクトにはロールがありません。私が逃したものについての助けは素晴らしいでしょう。ありがとう。

4

2 に答える 2

1

ロールプロバイダーが必要だと思います。メンバーシッププロバイダーがユーザーのメンバーシップを処理する方法、作成、削除、検証、編集と同様に、ロールを使用するには、RoleProvider(ASP.NETのロールプロバイダーの実装)を使用する必要があります。

これには、web.configでロールを有効にする必要もあります。次に例を示します。

<roleManager enabled="enabled" defaultProvider="AspNetSqlRoleProvider">
  <providers>
    <clear/>
      <add name="AspNetSqlRoleProvider" 
           type="System.Web.Security.SqlRoleProvider"
           connectionStringName="ApplicationServices" 
           applicationName="/" />
      <add name="AspNetWindowsTokenRoleProvider"
           type="System.Web.Security.WindowsTokenRoleProvider"
           applicationName="/" />
  </providers>
</roleManager>

これは便利かもしれません: SOasp-net-mvc-roles-without-database-and-without-role-provider

可能性があります: ASP.NET 2.0、「ロールプロバイダー」なしのカスタムロール割り当て

于 2010-08-11T01:45:14.473 に答える
1

アップデート:

最後に、変更してこれを機能させました

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    var cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

    if (cookie == null) return;
    var decryptedCookie = FormsAuthentication.Decrypt(cookie.Value);            
    var roles = decryptedCookie.UserData.Split('|');

    var tcmIdentity = new TcmIdentity(decryptedCookie.Name);
    var tcmPrincipal = new GenericPrincipal(tcmIdentity, roles);

    Context.User = tcmPrincipal;
}

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    var cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

    if (cookie == null) return;
    var decryptedCookie = FormsAuthentication.Decrypt(cookie.Value);            
    var roles = decryptedCookie.UserData.Split('|');

    var tcmIdentity = new TcmIdentity(decryptedCookie.Name);
    var tcmPrincipal = new GenericPrincipal(tcmIdentity, roles);

    Thread.CurrentPrincipal = Context.User = tcmPrincipal;
}
于 2010-08-11T06:27:45.543 に答える