在ASP.Net MVC中进行身份认证

 这种表单验证的方式适用于本地用户凭据,其优点是容易设置和便于管理。

@using (Html.BeginForm("Login", "Home"))
{ 
    <input type="text" name="adminName" />
    <br />
    <input type="password" name="pwd" />
    <input type="submit" name="name" value="登录" />
}

 

复制代码
        public ActionResult Login()
        {
            string name = HttpContext.Request.Form["adminName"];
            string pwd = HttpContext.Request.Form["pwd"];
            //为当前用户提供一个身份票据,并将该票据添加到cookie
            System.Web.Security.FormsAuthenticationTicket ticket = new System.Web.Security.FormsAuthenticationTicket(1, "vichin", System.DateTime.Now, System.DateTime.Now.AddMinutes(30), false, "vichin");
            //生成一个新的身份。
            System.Web.Security.FormsIdentity identity = new System.Web.Security.FormsIdentity(ticket); 
       System.Web.HttpCookie cookie = new System.Web.HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, System.Web.Security.FormsAuthentication.Encrypt(ticket));
            System.Web.Security.FormsAuthentication.SetAuthCookie("vichin", false);
            Response.Cookies.Add(cookie);
            //拿到用户输入的账号密码之后需要去后台数据库进行验证,若验证通过则继续。
            //先将通过验证的用户名存放到session中。
            Session["user"] = name;
            return RedirectToAction("Index", "Home");
        }

        public ActionResult DoSth()
        {            
            if (this.User.Identity.IsAuthenticated)//如果存在身份认证标识
            {
                //string adminName = this.User.Identity.Name;//获取写入的adminName。
            }
            return View();
        } 
复制代码
        public ActionResult LoginOut()
        {
            System.Web.Security.FormsAuthentication.SignOut();//这里删除凭据之后,需要将页面跳转到登录页面才行,否则this.User.Identity.IsAuthenticated仍然为true            
            Session.Abandon(); //清除session
            return RedirectToAction("Index", "Login");
        }

 

 

在Web.config文件中进行相关配置。过期时间为30分钟。跳转页面

复制代码
    <system.web>
      
      <authentication mode="Forms">
        <forms loginUrl="~/Home/Index" timeout="30"></forms>
      </authentication>
    <authorization>
      <allow users="*"/>
    </authorization>
    
  </system.web>
复制代码

 

 

 

如果微软提供的基本认证无法满足需求的时候,可以写自己的特性来继承AuthorizeAttribute类。

复制代码
    public class CheckPermissionAttribute : AuthorizeAttribute
    {
        //复写AuthorizeAttribute类中的虚方法AuthorizeCore,该方法会在授权认证时执行。
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (Users != "james")
            {
                return false;//这里return false表示认证授权未通过。
            }
            if (Roles != "NBAPlayer")
            {
                return false;
            }
            //这里可以使用httpContext对象来获取客户端发过来的所有的请求信息,根据这些信息,我们可以进行后续的认证。
            return base.AuthorizeCore(httpContext);
        }
    }
复制代码

在Action中的使用

        [CheckPermission(Users = "james", Roles = "NBAPlayer")]
        public ActionResult Index()
        {
            return Content("This is the Index action on the Home Controller");
        }

 

书上的大神不建议自己写类去实现IAuthorizationFilter。因为实现编写安全性的代码可能会存在一些遗漏的缺陷或未经测试的角落,会给应用程序留下安全漏洞。

 微软提供的默认authorize特性在小项目和中型的对权限控制没有复杂要求的项目里已经够用了。缺点是项目开发前得确定好业务的各种角色,因为要以“硬编码”的方式写在接口方法上。后期如果要修改一个接口的所属角色,只有重新修改代码。

posted @   水墨晨诗  阅读(695)  评论(2编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示