web-dev-qa-db-ja.com

ASP.NET MVC 4のカスタムメンバーシップおよびロールプロバイダー

ASP.NET MVC 4 Webアプリケーションを作成しています。私はカスタムメンバーシップについてググったが、良いリソースやビデオ講義を見つけることができなかった。

それらのほとんどは、古いリンクまたはデッドリンクです。メンバーシップおよびロールプロバイダーの作成を開始する方法について、いくつかのリソースを提案していただけませんか。

18
Mukesh Sharma

メンバーシップと役割について理解することは、私にとっても非常に困難でした。あなたがウェブ上で見つけることができる多くの信頼できる詳細なコンテンツはないからです。このトピックについて理解するためにいくつかのビデオを見てみましたが、はっきりしませんでした。しかし、Code Projectと呼ばれるウェブサイトからの2つの記事が救いに来ました。メンバーシップのカスタマイズに関するステップバイステップガイドを見ることができるこれらのリンクを共有しています

リンク1
リンク1は、ログイン認証のために電子メールをユーザー名に置き換えるのに役立ちます。これは、開発者がMicrosoft提供のIDモジュールで必要とする最も一般的なカスタマイズの1つです。

Link2

2番目の記事は、作成したユーザーに役割を追加および添付する方法と、ユーザー登録ページへのアクセスを管理者のみに制限する方法を理解するのに役立ちます。この2つの記事の助けを借りて、認証と承認の基本を理解していただければ幸いです。

1
Abdul Hannan

古いメンバーシップの代わりにASP.Net Identityを使用することをお勧めします。ASP.NetIdentityは、古いメンバーシップよりも優れており、柔軟性があります。また、アクションフィルターを使用してロールベースの認証をサポートし、独自のカスタマイズされたプロバイダー(ロールやユーザープロバイダー)。

以下のリンクを参照してください

https://weblog.west-wind.com/posts/2015/Apr/29/Adding-minimal-OWIN-Identity-Authentication-to-an-Existing-ASPNET-MVC-Application

http://www.c-sharpcorner.com/article/create-identity-in-simple-ways-using-asp-net-mvc-5/

1
Code_Worm
http://logcorner.com/how-to-configure-custom-membership-and-role-provider-using-asp-net-mvc4/

**for creating a CustomerMemberShipClass** your class must implement System.Web.Security.MembershipProvider abstarct class. and you override the method ValidateUser()
in this ValidateUser() you have to write your own logic based on which you want authenticate user and return true or false according to it.

Sample ValidateUser method 
  public override bool ValidateUser(string username, string password)
        {
           int count=db.GetAll().Where(x => x.UserEmail == username && x.password == password).Count();
           if (count != 0)
               return true;
           else
               return false;
        }

later in web.config file you have add the fallowing under <sytem.web> element


<membership defaultProvider="MyMembershipProvider">
      <providers>
        <clear/>
        <add name="MyMembershipProvider" type="Write your class name that is implementing membershipproviderclas"/>
      </providers>
    </membership>

after doing this you can validate user using **MemberShip.Validate(Username,password)** which returns true or false based on ur code in ValidateUser() in CustomMemberShipProvider class and this will also set **[Authorize] attribute**

**for creating a CustomRoleProviderClass** your class must inherit System.Web.Secuirty.RoleProvider and override the appropriate method to get the roles for the user

SAmple method for getting roles for user


 public override string[] GetRolesForUser(string username)
        {
            string[] str={db.GetAll().Where(x=>x.UserEmail==username).FirstOrDefault().Role};
            return str;
        }

after this you must add the fallowing in web.config file in <system.web> element

<roleManager enabled="true" defaultProvider="MyRoleProvider">
      <providers>
        <clear/>
        <add name="MyRoleProvider" type="BLL.DoctorAppointmentRoleProvider"/>
      </providers>
    </roleManager>

 and after this u can check the role of the user using attribute **[Authorize(role="admin"])** and in Razor view you can check using User.IsinROle("A").

ASP.NET MVC 4インターネットテンプレートは、SimpleMembershipの上に構築されたいくつかの新しい非常に便利な機能を追加します。これらの変更により、非常にシンプルで拡張可能なメンバーシップAPIやOAuthのサポートなど、いくつかの優れた機能が追加されます。ただし、新しいアカウント管理機能にはSimpleMembershipが必要であり、既存のASP.NETメンバーシッププロバイダーに対しては機能しません

ここでASP.NET Identityのリソースを確認してください。

http://www.asp.net/identity/overview/getting-started/aspnet-identity-recommended-resources

0
Ha Doan