![](/img/trans.png)
[英]Asp.net: Custom Role and Membership Provider, LoginStatus Control not reacting
[英]Custom Membership and Role provider in ASP.NET MVC 4
我正在創建一個ASP.NET MVC 4 Web應用程序。 我搜索了自定義會員資格,但我找不到好的資源或視頻講座。
他們中的大多數要么是過時的,要么是死鏈接。 請問您能否就如何開始編寫會員資格和角色提供者提供一些資源。
我建議使用ASP.Net Identity而不是舊的成員資格.ASP.Net Identity比舊的成員資格更好,更靈活,它還支持使用操作過濾器進行基於角色的身份驗證,並且您可以實現自己的自定義提供程序(例如角色和用戶提供商)。
見下面的鏈接
http://www.c-sharpcorner.com/article/create-identity-in-simple-ways-using-asp-net-mvc-5/
ASP.NET MVC 4 Internet模板添加了一些基於SimpleMembership構建的新的非常有用的功能。 這些更改添加了一些很棒的功能,例如更簡單和可擴展的成員資格API以及對OAuth的支持。 但是,新的帳戶管理功能需要SimpleMembership,並且不適用於現有的ASP.NET成員資格提供程序
在此處查看ASP.NET標識的資源:
http://www.asp.net/identity/overview/getting-started/aspnet-identity-recommended-resources
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").
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.