使用SqlMembershipProvider和自定义SQL数据库覆盖ValidateUser() [英] Overriding ValidateUser() with SqlMembershipProvider and custom SQL database

查看:71
本文介绍了使用SqlMembershipProvider和自定义SQL数据库覆盖ValidateUser()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用网络上已经使用的SQL数据库,因此我编写了重写 ValidateUser()的代码:

 公共重写bool ValidateUser(字符串用户名,字符串密码){返回true;} 

我已经对此进行了设置,以使其返回true,而现在不检查我的数据库,只是为了测试它的理论.

是我需要做的,还是有更多要做的事情,以便它可以与其他功能正常工作,例如存储用户名信息,以便以后可以检索它,或者我只是将这些信息存储在会话变量?

此函数返回true后,是否可以对用户进行有效身份验证?

解决方案

如果您已经从 ValidateUser (例如,记录无效的登录尝试),您无需手动存储用户(用户存储在数据库中并通过Cookie进行标识),并且可以通过 解决方案

You don't need to override or implement all of the methods if you already inherit from SqlmembershipProvider.

If you want to override ValidateUser (for example to log invalid login attempts), you don't need to store the user manually(he's stored in DB and identified via cookie) and can be retrieved by Membership.GetUser.

For example:

public override bool ValidateUser(string username, string password)
{
    // check in database with SqlmembershipProvider
    bool isValid = base.ValidateUser(username, password);
    // get user from database
    var user = Membership.GetUser(username);
    if(isValid)
    {
        // ...
    }
    else{
        // log wrong attempt if you want
    }
    return isValid;
}

这篇关于使用SqlMembershipProvider和自定义SQL数据库覆盖ValidateUser()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆