1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul Brown
  • 4,926
  • 9
  • 32
  • 44

1 Answers1

2

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);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • excellent :) however VS is not liking the line: using(DbContext yourCtx = new DbContext()) ...says 'System.Data.Entity.DbContext.DbContext()' is inaccessible due to its protection level Thanks Paul – Paul Brown Mar 25 '11 at 12:41
  • 1
    @Paul Brown: you need to replace `DbContext` with your concrete database context (I don't know what it's called - it should be a class deriving from `DbContext`) – marc_s Mar 25 '11 at 12:49
  • @ marc_s - Excellent, got that sorted. Now one last thing..."var profile" is giving "Cannot assign method group to an implicitly-typed local variable" any ideas? thanks so much for your help! – Paul Brown Mar 25 '11 at 12:58
  • 1
    @Paul Brown: oh, sorry - my bad - the `.SingleOrDefault()` is a method and thus needs a pair of parens at the end... – marc_s Mar 25 '11 at 13:37