1

私は ASP.NET MVC でメンバーシップ プロバイダーを使用しており、ほとんどのデータ アクセスには nHibernate とリポジトリ パターンを使用しています。メンバーシップ プロバイダーよりも Facade を使用することをお勧めしますか?リポジトリを作成して、エンティティ モデルの残りの部分ともう少しインラインで動作させることができますか? また、関数をロールに追加する機能などの追加機能も追加しました。ファサードを作成すると、クラスが少し良くなります。

他の人はメンバーシップ プロバイダーで何をしましたか?

4

2 に答える 2

0

私はこの正確な問題を解決しました。次のように:

web.config

<authentication mode="Forms">
  <forms name="APPAUTH"
         defaultUrl="/webapp/Home.mvc"
         loginUrl="/webapp/Session.mvc/Login"
         protection="All"
         timeout="30"
         path="/"/>
</authentication>
<authorization>
  <deny users="?"/>
</authorization>

<location path="Session">
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>

Application_AuthenticateRequest次に、次の線に沿って何かをフックします。

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Context.Request.Cookies[cookieName];

    if (null == authCookie)
    {
        //no authentication cokie present
        return;
    }

    FormsAuthenticationTicket authTicket = null;
    try
    {
        authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    }
    catch (Exception)
    {
        // Can't do anything if we can't decrypt the ticket so treat it as not there
        FormsAuthentication.SignOut(); // Remove bad ticket
    }

    if (authTicket == null)
    {
        //could not decrypt cookie
        return;
    }

    // get the role
    string[] roles = authTicket.UserData.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

    // Set the security context
    ISecurityService security = ContainerProvider.RequestContainer.Resolve<ISecurityService>();
    Models.User user = security.GetUser(authTicket.Name);

    if (user == null)
    {
        FormsAuthentication.SignOut();
        throw new HttpException((int)System.Net.HttpStatusCode.Unauthorized, "Session expired!");
    }

    AppIdentity id = new AppIdentity(user, !authTicket.Expired);
    AppPrincipal principal = new AppPrincipal(id, roles);

    Context.User = principal;
} 

AutofacコンテナへのContainerProvider.RequestContainer.Resolve<ISecurityService>();呼び出しですが、ここで必要なことややりたいことは何でもできます。

AppIdentityとクラスはカスタムなので、AppPrincipal自分の役割にアクセスできますが、それほど複雑ではありません。

于 2009-05-01T19:19:43.210 に答える
0

MembershipProvider 基本クラスを継承することで、サイトに独自の MembershipProvider を実装できます。次に、nHibernate を介してデータ アクセスを実行するために必要なメソッドをオーバーライドします。

実際に実装する必要がある関数は ValidateUser だけです。残りは、MembershipProvider に関連するサイトで使用する機能によって異なります。

于 2011-02-11T20:28:52.973 に答える