Validating the user with Membership Provider
I'm customizing a form of validation of the users in my application Asp.net MVC 3.
How can I imple开发者_如何学Pythonment the method ValidateUser
?
My problem is the password for the MembershipUser
class (which I also customize) has a Password property.
I'm using EF CodeFirst .. following code:
MembershipUser
public class User : MembershipUser
{
public User(string username, object providerUserKey, string email, string passwordQuestion, bool isApproved,
bool isLockedOut)
: base("", username, providerUserKey, email, passwordQuestion, "", isApproved, isLockedOut, DateTime.Now, DateTime.MinValue,
DateTime.Now, DateTime.MinValue, DateTime.MinValue)
{
}
}
MembershipProvider
public class UserProvider : MembershipProvider
{
public override bool ValidateUser(string email, string password)
{
var bytes = new ASCIIEncoding().GetBytes(password);
var encryptedPassword = EncryptPassword(bytes);
using (var db = new DataContext())
{
var user = from u in db.Users
where u.Email == email
/* How to compare password? */
}
}
}
Please, is there a complete article with the implementation of this class?
I have not got a complete example but will this not work for you?
public class UserProvider : MembershipProvider
{
public override bool ValidateUser(string email, string password)
{
var bytes = new ASCIIEncoding().GetBytes(password);
var encryptedPassword = EncryptPassword(bytes);
var user = db.Users.FirstOrDefault(x =>
x.EmailAddress == emailAddress && x.Password == encryptedPassword);
return user != null;
}
}
精彩评论