[Asp.Net MVC4]驗證使用者登入實現

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   strong   for   

控制器:

HomeController 這是首頁的控制器

LoginController 這是登陸的控制器

類:

CDBTemplate.cs 這是資料庫資料對應的類,裡邊描述的是資料庫的結構

首先在HomeController 控制器的返回函數

[csharp] view plaincopy
  1. public ActionResult Index(){...}  

前面加上:

[csharp] view plaincopy
  1. [Authorize(Roles = "admins")]  

就是這樣:

[csharp] view plaincopy
  1. [Authorize(Roles = "admins")]  
  2. public ActionResult Index()  
  3. {  
  4.     ...  
  5. }  

這條語句的意思是在這加上一個許可權驗證,只允許使用者角色是admins的使用者訪問

然後再web.config檔案裡添加:

[csharp] view plaincopy
  1. <authentication mode="Forms">  
  2.       <forms loginUrl="~/Login" timeout="2880" />  
  3. </authentication>  

這些的意思是給整個網站增加使用者驗證,指向的登陸介面是login這個控制器

CDBTemplate.cs檔案裡的一個類:

[csharp] view plaincopy
  1. public class LogOnModel  
  2.     {  
  3.         [Required]  
  4.         [Display(Name = "使用者名稱")]  
  5.         public string UserName { get; set; }  
  6.   
  7.   
  8.         [Required]  
  9.         [DataType(DataType.Password)]  
  10.         [Display(Name = "密碼")]  
  11.         public string Password { get; set; }  
  12.   
  13.   
  14.         [Display(Name = "下次自動登陸")]  
  15.         public bool RememberMe { get; set; }  
  16.     } 

然後為LoginController 控制器的預設返回函數增加一個視圖Index.cshtml,在頁面裡面加上下面的代碼:

[csharp] view plaincopy
  1. @model Weibo.Models.LogOnModel //LogOnModel 是CDBTemplate.cs檔案裡的一個類  
  2. @using (Html.BeginForm("Login","Login",FormMethod.Post)) {  
  3.     @Html.TextBoxFor(m => m.UserName)  
  4.                 @Html.ValidationMessageFor(m => m.UserName, "請輸入使用者名稱!", new {style="color: #f00" })  
  5. @Html.PasswordFor(m => m.Password)  
  6.                 @Html.ValidationMessageFor(m => m.Password,"請輸入密碼!",new {style="color: #f00" })  
  7. @Html.CheckBoxFor(m => m.RememberMe)  
  8.                 @Html.LabelFor(m => m.RememberMe)  
  9. @Html.ActionLink("忘記密碼", "forgotpwd", null, new {@class="rt",target="_blank" })  
  10. <input type="submit" value="登陸微博" />  
  11. } 

在上面的代碼裡Html.BeginForm("Login","Login",FormMethod.Post)方法的第一個參數的意思是指定要調用的 控制器的方法的名字,第二個參數的意思是控制器的名字,第三個參數的意思是用什麼方法把表單提交給伺服器,這裡我們為了安全,選擇用post方式提交。

然後在LoginController 控制器中增加這麼一個方法:

[csharp] view plaincopy
  1. [HttpPost, ActionName("Login")]  
  2.         public void Login(FormCollection collection)  
  3.         {  
  4.             object obj = SqlHelper.ExecuteScalar("select UserId from CDBUsers where [email protected] and [email protected]",  
  5.                  new SqlParameter("@uname", collection[0]),  
  6.                  new SqlParameter("@pwd", Weibo.Models.Myencrypt.myencrypt(collection[1])));  
  7.   
  8.   
  9.             if (obj != null)  
  10.             {  
  11.                 FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(  
  12.                     1,  
  13.                     collection[0],  
  14.                     DateTime.Now,  
  15.                     DateTime.Now.AddMinutes(30),  
  16.                     false,  
  17.                     "admins"  
  18.                     );  
  19.                 string encryptedTicket = FormsAuthentication.Encrypt(authTicket);  
  20.                 System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);  
  21.                 System.Web.HttpContext.Current.Response.Cookies.Add(authCookie);  
  22.             }  
  23.   
  24.   
  25.             Response.Redirect("~/");  
  26.         } 

 

[Asp.Net MVC4]驗證使用者登入實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.