標籤:cWeb style blog http color io ar strong for
asp.net MVC 內建的membershipProvider可以實現使用者登陸驗證,但是它用的是自動建立的資料庫,所以你想用本機資料庫資料去驗證,是通過不了的。
如果我們想用自己的資料庫的話,可以寫自己的membershipProvider!下面介紹如果建立自己的membershipProvider:
1.寫自己的MembershipProvider類,這個類繼承自命名空間System.Web.Security下的MembershipProvider類
這個類放在哪無所謂,這裡我放在建立MyCode檔案夾裡,然後建立類,取名為MyMembershipProvider,代碼如下:
using System.Web.Security;namespace MvcWeb.MyCode{ //自訂類,繼承System.Web.Security.MembershipProvider public class MyMembershipProvider: MembershipProvider { }}
現在加代碼進去,把滑鼠游標放在MembershipProvider上,右鍵選擇“實現抽象類別”
實現抽象類別後會有很多方法,這裡只用到最後一個方法代碼如下:
using System.Data.SqlClient;namespace MvcFeiGeTe.MyCode{ //自訂類,繼承System.Web.Security.MembershipProvider public class MyMembershipProvider: MembershipProvider { public override bool ValidateUser(string username, string password) { throw new NotImplementedException(); } }}
下面是填充上面方法的代碼:
public override bool ValidateUser(string username, string password) { SqlConnection sqlconn = new SqlConnection("Data Source=.;Initial Catalog=MyDb;Integrated Security=True;user id=sa;password=admin123"); SqlCommand sqlcmd = new SqlCommand("select userName,passWord from adminInfo where userName = @userName and passWord = @passWord", sqlconn); try { sqlconn.Open(); sqlcmd.Parameters.Add(new SqlParameter("@userName", SqlDbType.NVarChar, 30)); sqlcmd.Parameters["@userName"].Value = username.Trim(); sqlcmd.Parameters.Add(new SqlParameter("@passWord", SqlDbType.NVarChar, 50)); sqlcmd.Parameters["@passWord"].Value = password.Trim(); SqlDataReader sqlRd = sqlcmd.ExecuteReader(); if (sqlRd.HasRows) { return true; } return false; } catch (Exception ex) { throw new Exception(ex.Message); } }
2.配置Web.config
把此設定檔下的<system.web>節點下的預設的<membership>節點換成如下(注意底線部分):
<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="10" applicationName="/" /> </providers> </membership>
3.驗證
在AccountController中:
if (ModelState.IsValid) { string password = FormsAuthentication.HashPasswordForStoringInConfigFile(model.passWord, "md5"); //加密 MyMembershipProvider mmsp = new MyMembershipProvider(); //自訂MyMembershipProvider,繼承mvc內建的MembershipProvider,實現登陸驗證,添加引用using MvcWeb.MyCode; if (mmsp.ValidateUser(model.userName, password)) { FormsAuthentication.SetAuthCookie(model.userName, true); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Admin"); } } else { ModelState.AddModelError("", "使用者名稱或密碼輸入有誤"); } }
ok!
asp.net MVC中如何用Membership類和自訂的資料庫進行登入驗證