I am making a new web application in ASP.NET (in C#) using FormsAuthentication for the login. A standard FormsAuthentication setup is in place currently, with usernames and passwords specified in the web.config as usual, and this setup is working great. However,
I would like to link this login setup to an SQL Server table in the database. This table stores user account informa开发者_JS百科tion for each user, including username and password. Basically, I would like to find a way so that the following line of code will look for a username and password in this DB table, rather than in the web.config as it normally does:
FormsAuthentication.Authenticate(txt_username.Text, txt_password.Text);
But I'm at a loss as to how to do this exactly. I've been reading about Membership.ValidateUser, but I still cannot find how to use that to link to the usernames and passwords in the users table in the database.
Any help will be highly appreciated! (Btw, this is my first post, so please feel free to point out any noob mistakes I may have made).
You need to implement a custom MembershipProvider.
The MembershipProvider
is the mechanism that your application will use to authenticate users as well as handle user-related functionality (change password, unlock users, etc). A custom MembershipProvider is nothing more than a class which extends the MembershipProvider
abstract class. This is where you'll write custom logic to interface with your database to perform the required membership actions (validate credentials, create user, change password, etc).
Have a look at this tutorial:
http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider
This is a good resource if you are unfamiliar with ASP.NET Membership.
精彩评论