欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

下面說(shuō)一下主要的結(jié)構(gòu)

控制器:

HomeController 這是主頁(yè)的控制器

LoginController 這是登陸的控制器

類:

CDBTemplate.cs 這是數(shù)據(jù)庫(kù)數(shù)據(jù)對(duì)應(yīng)的類,里邊描述的是數(shù)據(jù)庫(kù)的結(jié)構(gòu)

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

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

public ActionResult Index(){...} 

前面加上:

[Authorize(Roles = "admins")] 

就是這樣:

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

這條語(yǔ)句的意思是在這加上一個(gè)權(quán)限驗(yàn)證,只允許用戶角色是admins的用戶訪問(wèn)

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

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

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

CDBTemplate.cs文件里的一個(gè)類:

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

然后為L(zhǎng)oginController 控制器的默認(rèn)返回函數(shù)增加一個(gè)視圖Index.cshtml,在頁(yè)面里面加上下面的代碼:

@model Weibo.Models.LogOnModel //LogOnModel 是CDBTemplate.cs文件里的一個(gè)類 
@using (Html.BeginForm("Login","Login",FormMethod.Post)) { 
  @Html.TextBoxFor(m => m.UserName) 
        @Html.ValidationMessageFor(m => m.UserName, "請(qǐng)輸入用戶名!", new {style="color: #f00" }) 
@Html.PasswordFor(m => m.Password) 
        @Html.ValidationMessageFor(m => m.Password,"請(qǐng)輸入密碼!",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)方法的第一個(gè)參數(shù)的意思是指定要調(diào)用的控制器的方法的名字,第二個(gè)參數(shù)的意思是控制器的名字,第三個(gè)參數(shù)的意思是用什么方法把表單提交給服務(wù)器,這里我們?yōu)榱税踩x擇用post方式提交。

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

[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("~/"); 
    } 

好了,搞定了~~~~

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論