[Asp.Net MVC4]验证用户登录实现实例

所属分类: 网络编程 / ASP.NET 阅读数: 1448
收藏 0 赞 0 分享

最近我们要做一个仿sina的微博,碰巧的是我最近在学习mvc,就想用mvc技术实现这个项目。

既然是微博,那不用想也应该知道肯定要有用户登陆,但是和常规的asp.NET登陆又不一样,以下是我一下午+一晚上的研究成果~~~

首先,建好数据库以及表,这就不用说了吧。

下面说一下主要的结构

控制器:

HomeController 这是主页的控制器

LoginController 这是登陆的控制器

类:

CDBTemplate.cs 这是数据库数据对应的类,里边描述的是数据库的结构

////////////////////////////////////////////我是分割线\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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

public ActionResult Index(){...} 

前面加上:

[Authorize(Roles = "admins")] 

就是这样:

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

这条语句的意思是在这加上一个权限验证,只允许用户角色是admins的用户访问

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

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

这些的意思是给整个网站增加用户验证,指向的登陆界面是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 控制器的默认返回函数增加一个视图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)方法的第一个参数的意思是指定要调用的控制器的方法的名字,第二个参数的意思是控制器的名字,第三个参数的意思是用什么方法把表单提交给服务器,这里我们为了安全,选择用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("~/"); 
    } 

好了,搞定了~~~~

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

ASP.NET 水晶报表打印功能实现代码

ASP.NET下的水晶报表打印,据我所知有以下几种办法可以打印
收藏 0 赞 0 分享

ASP.Net 图片存入数据库的实现代码

在很多时候,我们有这样的需求:把图片存入到数据库当中。在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在文件系统中。
收藏 0 赞 0 分享

让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)

Microsoft Expression Blend 2 制作动画个人感觉倒像3DMAX 可以自动捕捉关键帧
收藏 0 赞 0 分享

asp.net Reporting Service在Web Application中的应用

由于我们这个项目中使用微软的报表服务(Reporting Services)作为报表输出工具,本人也对它进行一点点研究,虽没有入木三分,但这点知识至少可以在大部分Reporting Service的场景中应用。
收藏 0 赞 0 分享

C# 文件上传 默认最大为4M的解决方法

.net中默只能上传小于4m的文件,大于4M将无法显示页面.那么如何设置来使imputfile能上传更大的文件呢
收藏 0 赞 0 分享

asp.net 购物车实现详细代码

asp.net 购物车实现详细代码
收藏 0 赞 0 分享

asp.net repeater实现批量删除时注册多选框id到客户端

repeater批量删除时注册多选框id到客户端的实现代码
收藏 0 赞 0 分享

asp.net aspnetpager分页统计时与实际不符的解决办法

最近分页方面根据实际需要修改了一些函数
收藏 0 赞 0 分享

iis 服务器应用程序不可用的解决方法

访问页面时提示 服务器应用程序不可用,大家可以按照下面的方法重新注册下,应该能好点
收藏 0 赞 0 分享

asp.net button 绑定多个参数

asp.net button 绑定多个参数的代码
收藏 0 赞 0 分享
查看更多