开发者

Validating the user with Membership Provider

开发者 https://www.devze.com 2023-03-24 02:39 出处:网络
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?

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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消