The built-in MembershipProvider of ASP. NET-MVC can implement user login verification, but it uses automatically created database, so you want to use local database data to verify, it is not.
If we want to use our own database, we can write our own membershipprovider!. The following describes if you create your own MembershipProvider:
1. Write your own MembershipProvider class, this class inherits from the MembershipProvider class under the namespace System.Web.Security
It doesn't matter where this class is placed, here I put in the new Mycode folder, then the new class, named Mymembershipprovider, the code is as follows:
using System.Web.Security; namespace mvcweb.mycode{ // custom class, inheriting System.Web.Security.MembershipProviderpublic class mymembershipprovider:membershipprovider { }}
Now add the code in, put the mouse cursor on the MembershipProvider, right-click to select "Implement abstract class"
There are many ways to implement an abstract class, where the last method code is used as follows:
using System.Data.SqlClient; namespace mvcfeigete.mycode{ // custom classes, inheriting System.Web.Security.MembershipProvider public class Mymembershipprovider:membershipprovider { public override bool ValidateUser (string username, string Span style= "color: #000000;" > password) { throw new NotImplementedException (); } }}
Here is the code to populate the above method:
Public Override BOOLValidateUser (stringUsernamestringpassword) {SqlConnection sqlconn=NewSqlConnection ("Data Source=.;i Nitial catalog=mydb;integrated security=true;user id=sa;password=admin123"); SqlCommand sqlcmd=NewSqlCommand ("Select Username,password from admininfo where userName = @userName and PassWord = @passWord", sqlconn); Try{sqlconn. Open (); sqlcmd. Parameters.Add (NewSqlParameter ("@userName", SqlDbType.NVarChar, -)); sqlcmd. parameters["@userName"]. Value =username. Trim (); sqlcmd. Parameters.Add (NewSqlParameter ("@passWord", SqlDbType.NVarChar, -)); sqlcmd. parameters["@passWord"]. Value =password. Trim (); SqlDataReader sqlrd=sqlcmd. ExecuteReader (); if(sqlrd.hasrows) {return true; } return false; } Catch(Exception ex) {Throw NewException (ex. Message); } }
2. Configure the Web. config
Replace the default <membership> node under the <system.web> node under this configuration file with the following (note the underscore section):
<membership defaultprovider="Mymembershipprovider"> <providers> <add name="Mymembershipprovider"Type=" MvcWeb.MyCode.MyMembershipProvider, System.Web.Providers, version=1.0.0.0, Culture=neutral, Publickeytoken=31bf3856ad364e35"Connectionstringname="defaultconnection"Enablepasswordretrieval="false"enablepasswordreset="true"Requiresquestionandanswer="false"Requiresuniqueemail="false"maxinvalidpasswordattempts="5"Minrequiredpasswordlength="6"minrequirednonalphanumericcharacters="0"passwordattemptwindow="Ten"Applicationname="/"/> </providers> </membership>
3. Verification
In the AccountController:
if(modelstate.isvalid) {stringPassword = formsauthentication.hashpasswordforstoringinconfigfile (Model.password,"MD5");//EncryptMymembershipprovider Mmsp =NewMymembershipprovider ();//custom Mymembershipprovider, inherit the membershipprovider of MVC, implement login verification, add reference using Mvcweb.mycode; if(Mmsp. ValidateUser (model.username, password)) {Formsauthentication.setauthcookie (Model.userna Me,true); if(Url.islocalurl (RETURNURL) && returnurl.length >1&& Returnurl.startswith ("/") &&!returnurl.startswith ("//") &&!returnurl.startswith ("/\\")) { returnRedirect (RETURNURL); } Else { returnRedirecttoaction ("Index","Admin"); } } Else{modelstate.addmodelerror ("","user name or password entered incorrectly"); } }
Ok!
How to use the Membership class and the custom database for login verification in ASP.