Implement custom “ValidateUser” in MembershipProvider

送分小仙女□ 提交于 2020-01-16 05:11:46

问题


I am implementing a custom MembershipProvider and I am trying to get the ValidateUser method to validate against my Profiles table in SQL Server. This table has columns called UserName and Password.

public override bool ValidateUser(string username, string password)
{
    ??? what to do here???
}

FYI, I am using MVC3 & EF 4.1 Code First.

Thanks

Paul


回答1:


If you're using EF 4.1, you will have some kind of a DbContext object that contains the DbSet for your Profiles table - right?

So in that case, use this:

public override bool ValidateUser(string username, string password)
{
    using(DbContext yourCtx = new DbContext())
    {
        // from your "Profiles" DbSet, retrieve that single entry which
        // matches the username/password being passed in

        var profile = (from p in yourCtx.Profiles
                      where p.UserName == username && p.Password == password
                      select p).SingleOrDefault();

        // if that query returns a "Profile" (is != null), then your
        // username/password combo is valid - otherwise, it's not valid
        return (profile != null);
    }
}


来源:https://stackoverflow.com/questions/5431243/implement-custom-validateuser-in-membershipprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!