Overriding ValidateUser() with SqlMembershipProvid

2019-08-06 01:27发布

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条回答
成全新的幸福
2楼-- · 2019-08-06 02:06

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;
}
查看更多
登录 后发表回答