با سلام من برای سطوح دسترسی از membership و roleManager استفاده میکنم اینم وب کانفیگم
<membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="60"> <providers> <clear /> <add name="MyMembershipProvider" type="ProjectShop.Classes.MyMembershipProvider" connectionStringName="dbgenericlabEntities" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" /> </providers> </membership> <roleManager defaultProvider="MyRoleProvider" enabled="true"> <providers> <clear /> <add name="MyRoleProvider" type="ProjectShop.Classes.MyRoleProvider" applicationName="/" /> </providers> </roleManager>
همه چی خوبه نکته اینجاست توو وردم اینجوری چک میکنم
[HttpPost] [AllowAnonymous] public ActionResult Login(grdUser user, string ReturnUrl = "") { UserRepository blUser = new UserRepository(); var isValidUser = Membership.ValidateUser(user.User_UserName, user.User_Password); if (isValidUser) { FormsAuthentication.SetAuthCookie(user.User_UserName, user.User_Status); if (Url.IsLocalUrl(ReturnUrl)) { return Redirect(ReturnUrl); } else { return RedirectToAction("Index", "Admin"); } }
همه چی خوبه اما وقتی یه نام کاربری : admin@yahoo.com میزنم یعنی هر نام کاربری که شامل @ و یا _ باشه وقتی ورود رو میزنه این پیغام رو میده
علتش فکر میکنید چیه؟ یعنی من نمیتونم توو نام کاربریم _ یا @ و .. بزارم؟
این خطا به صورت دستی صادر می شود و در اینجا به این منظور است که MemberShipPasswordFormat پیاده سازی نشده است.
متوجه نشدم یعنی باید چیکار کنم؟
الان هر کاری میکنم میگه نام کاربری باید شامل @ و ... نباشه
این کلاسه من هست باید تغییر ش بدم؟
using ProjectShop.Models.DomainModels; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; namespace ProjectShop.Classes { public class MyMembershipProvider : MembershipProvider { public override string ApplicationName { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public override bool ChangePassword(string username, string oldPassword, string newPassword) { throw new NotImplementedException(); } public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer) { throw new NotImplementedException(); } public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { throw new NotImplementedException(); } public override bool DeleteUser(string username, bool deleteAllRelatedData) { throw new NotImplementedException(); } public override bool EnablePasswordReset { get { throw new NotImplementedException(); } } public override bool EnablePasswordRetrieval { get { throw new NotImplementedException(); } } public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords) { throw new NotImplementedException(); } public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) { throw new NotImplementedException(); } public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) { throw new NotImplementedException(); } public override int GetNumberOfUsersOnline() { throw new NotImplementedException(); } public override string GetPassword(string username, string answer) { throw new NotImplementedException(); } public override MembershipUser GetUser(string username, bool userIsOnline) { throw new NotImplementedException(); } public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) { throw new NotImplementedException(); } public override string GetUserNameByEmail(string email) { throw new NotImplementedException(); } public override int MaxInvalidPasswordAttempts { get { throw new NotImplementedException(); } } public override int MinRequiredNonAlphanumericCharacters { get { throw new NotImplementedException(); } } public override int MinRequiredPasswordLength { get { throw new NotImplementedException(); } } public override int PasswordAttemptWindow { get { throw new NotImplementedException(); } } public override MembershipPasswordFormat PasswordFormat { get { throw new NotImplementedException(); } } public override string PasswordStrengthRegularExpression { get { throw new NotImplementedException(); } } public override bool RequiresQuestionAndAnswer { get { throw new NotImplementedException(); } } public override bool RequiresUniqueEmail { get { throw new NotImplementedException(); } } public override string ResetPassword(string username, string answer) { throw new NotImplementedException(); } public override bool UnlockUser(string userName) { throw new NotImplementedException(); } public override void UpdateUser(MembershipUser user) { throw new NotImplementedException(); } // Here In this example we will use only ValidateUser method, we will see remaining later like create user, //update user change password and more public override bool ValidateUser(string username, string password) { //Will write code for validate user from our own database using(dbgenericlabEntities dc = new dbgenericlabEntities()) { var user = dc.User.Where(a => a.User_UserName.Equals(username) && a.User_Password.Equals(password)).FirstOrDefault(); if (user != null) { return true; } } return false; } } }
من مبتدی ام یعنی خیلی حرفه ایی نیستم ممنون میشم دقیق بگید چیکار کنم
باید در وب کانفیگ ست کنید
تنظیمات به شرح زیر است
<configuration> <connectionStrings> <add name="MySqlConnection" connectionString="Data Source=MySqlServer;Initial Catalog=aspnetdb;Integrated Security=SSPI;" /> </connectionStrings> <system.web> <authentication mode="Forms" > <forms loginUrl="login.aspx" name=".ASPXFORMSAUTH" /> </authentication> <authorization> <deny users="?" /> </authorization> <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15"> <providers> <clear /> <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MySqlConnection" applicationName="MyApplication" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" requiresUniqueEmail="true" passwordFormat="Hashed" /> </providers> </membership> </system.web> </configuration>
متدها و پراپرتی هایی که شامل NotImplementedException هستند را پاک کنید تا از اجزای اصلی خود استفاده کند.
آقای مدائنی کدی که شما گفتید رو توو وی کانفیگ گذاشتم اما توو اون اصلاً مشخص نیست که membership من چه کلاسی و کجاست به خاط همین وقتی اجرا کردم اصلاً validateuser که در کلاس Membership هست رو نخووند
آقای علیزاده من چون کلاسم اینجوریه
MyMembershipProvider : MembershipProvider
یعنی ارث بری میکنه وقتی اون هایی که شامل NotImplementedException هستند رو پاک میکنم بهم ارور میده
یه سوال نمیشه من اصلا از membership استفاده نکنم فقط از roleManager , roleprovider استفاده کنم؟
من role ها برام خیلی مهمه
به این صورت عمل کنید:
public override MembershipPasswordFormat PasswordFormat { get; }
آقای علیزاده بازم ارور داد
کلا membership رو بیخیال شدم و فقط از RoleProvider استفاده کردم درست شد
باز ممنونم
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)