使用会员提供商验证用户

发布于 2024-11-26 19:52:15 字数 1238 浏览 2 评论 0原文

我正在我的应用程序 Asp.net MVC 3 中自定义一种用户验证形式。

如何实现 ValidateUser 方法?

我的问题是 MembershipUser 类(我也自定义)的密码有一个 Password 属性。

我正在使用 EF CodeFirst .. 以下代码:

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? */
        }
    }

}

请问,是否有关于此类实现的完整文章?

I'm customizing a form of validation of the users in my application Asp.net MVC 3.

How can I implement 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如荒岛 2024-12-03 19:52:15

我还没有完整的例子,但这对你不起作用吗?

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文