Overriding ValidateUser() with SqlMembershipProvider and custom SQL database

烂漫一生 提交于 2019-12-11 06:26:48

问题


I wish to use an SQL database that it already in use on my network, so I have written code to override ValidateUser():

public override bool ValidateUser(string username, string password)
{
    return true;
}

I've set this so that it returns true, without checking my database for now, just to test the theory of it.

Is that all I need to do, or is there more to do so that it works properly with the other functions, like storing the username information so that it can be retrieved later on, or do I simply store this information in a session variable?

Once this function returns true, it the user effectively authenticated?


回答1:


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


来源:https://stackoverflow.com/questions/12094295/overriding-validateuser-with-sqlmembershipprovider-and-custom-sql-database

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