亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

[Asp.Net MVC4]驗證用戶登錄實現(xiàn)實例

 更新時間:2016年12月07日 09:54:29   作者:Hsppl  
這篇文章主要介紹了[Asp.Net MVC4]驗證用戶登錄實現(xiàn)實例,這里整理了詳細的代碼,具有一定的參考價值,有需要的小伙伴可以參考下。

最近我們要做一個仿sina的微博,碰巧的是我最近在學習mvc,就想用mvc技術實現(xiàn)這個項目。

既然是微博,那不用想也應該知道肯定要有用戶登陸,但是和常規(guī)的asp.NET登陸又不一樣,以下是我一下午+一晚上的研究成果~~~

首先,建好數(shù)據(jù)庫以及表,這就不用說了吧。

下面說一下主要的結構

控制器:

HomeController 這是主頁的控制器

LoginController 這是登陸的控制器

類:

CDBTemplate.cs 這是數(shù)據(jù)庫數(shù)據(jù)對應的類,里邊描述的是數(shù)據(jù)庫的結構

////////////////////////////////////////////我是分割線\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

首先在HomeController 控制器的返回函數(shù)

public ActionResult Index(){...} 

前面加上:

[Authorize(Roles = "admins")] 

就是這樣:

[Authorize(Roles = "admins")] 
public ActionResult Index() 
{ 
  ... 
} 

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

然后再web.config文件里添加:

<authentication mode="Forms"> 
   <forms loginUrl="~/Login" timeout="2880" /> 
</authentication> 

這些的意思是給整個網(wǎng)站增加用戶驗證,指向的登陸界面是login這個控制器

CDBTemplate.cs文件里的一個類:

public class LogOnModel 
  { 
    [Required] 
    [Display(Name = "用戶名")] 
    public string UserName { get; set; } 
 
 
    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "密碼")] 
    public string Password { get; set; } 
 
 
    [Display(Name = "下次自動登陸")] 
    public bool RememberMe { get; set; } 
  } 

然后為LoginController 控制器的默認返回函數(shù)增加一個視圖Index.cshtml,在頁面里面加上下面的代碼:

@model Weibo.Models.LogOnModel //LogOnModel 是CDBTemplate.cs文件里的一個類 
@using (Html.BeginForm("Login","Login",FormMethod.Post)) { 
  @Html.TextBoxFor(m => m.UserName) 
        @Html.ValidationMessageFor(m => m.UserName, "請輸入用戶名!", new {style="color: #f00" }) 
@Html.PasswordFor(m => m.Password) 
        @Html.ValidationMessageFor(m => m.Password,"請輸入密碼!",new {style="color: #f00" }) 
@Html.CheckBoxFor(m => m.RememberMe) 
        @Html.LabelFor(m => m.RememberMe) 
@Html.ActionLink("忘記密碼", "forgotpwd", null, new {@class="rt",target="_blank" }) 
<input type="submit" value="登陸微博" /> 
}

在上面的代碼里Html.BeginForm("Login","Login",FormMethod.Post)方法的第一個參數(shù)的意思是指定要調用的控制器的方法的名字,第二個參數(shù)的意思是控制器的名字,第三個參數(shù)的意思是用什么方法把表單提交給服務器,這里我們?yōu)榱税踩?,選擇用post方式提交。

然后在LoginController 控制器中增加這么一個方法:

[HttpPost, ActionName("Login")] 
    public void Login(FormCollection collection) 
    { 
      object obj = SqlHelper.ExecuteScalar("select UserId from CDBUsers where UserName=@uname and Password=@pwd", 
         new SqlParameter("@uname", collection[0]), 
         new SqlParameter("@pwd", Weibo.Models.Myencrypt.myencrypt(collection[1]))); 
 
 
      if (obj != null) 
      { 
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 
          1, 
          collection[0], 
          DateTime.Now, 
          DateTime.Now.AddMinutes(30), 
          false, 
          "admins" 
          ); 
        string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
        System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); 
        System.Web.HttpContext.Current.Response.Cookies.Add(authCookie); 
      } 
 
 
      Response.Redirect("~/"); 
    } 

好了,搞定了~~~~

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論