在的MembershipProvider

在的MembershipProvider

本文介绍了实现自定义"&的ValidateUser QUOT;在的MembershipProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个自定义的MembershipProvider,我试图让的ValidateUser 方法来验证我的配置文件表在SQL Server中。此表有一个名为用户名密码

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

仅供参考,我使用MVC3&安培; EF 4.1 code首先

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

感谢

推荐答案

如果你使用EF 4.1中,你将有某种包含一个的DbContext 对象 DbSet 配置文件表 - 右

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

这篇关于实现自定义"&的ValidateUser QUOT;在的MembershipProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:14