问题
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