Asp.Net MVC 身份认证

Asp.Net MVC 登录验证:

1. Windows身份认证,主要用于Intranet上域环境。使用Windows验证时,用户的Windows安全令牌在用户访问整个网站期间使用HTTP请求,进行消息发送。应用程序会使用这个令牌在本地(或者域)里验证用户账号的有效性,也会评估用户所在角色所具备的权限。当用户验证失败或者未授权时,浏览器就会定向到特定的页面让用户输入自己的安全凭证(用户名和密码)。

    Windows身份认证的局限性非常明显,一旦用户有超出本地域控制器范围的外网用户访问网站,就会出现问题

2. Forms身份认证,应用于Internet。ASP.NET需要验证加密的HTTP cookie或者查询字符串来识别用户的所有请求。cookie与ASP.NET会话机制(session)的关系密切,在会话超时或者用户关闭浏览器之后,会话和cookie就会失效,用户需要重新登录网站建立新的会话。

3. ASP.NET Identity - Claims-based(基于声明)的身份认证:   对比Windows认证和Forms身份认证,claims-based认证这种方式将认证和授权与登录代码分开,将认证和授权拆分成另外的web服务。类似于使用的是第三方的登录系统,如微信登录。

 

使用Forms身份验证流程:

一. 配置:

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

 

二、用户登录

  1、用户名密码验证。

  2、验证成功,取出部分必要的用户信息usedata,序列化成string.

       3. 创建FormsAuthenticationTicket 票据,将usedata放入其中。

        public void SetCookie(string source)
        {

            var ticket = new FormsAuthenticationTicket(1, "loginuser", DateTime.Now, DateTime.Now.AddDays(10), false, source);

            string authTicket = FormsAuthentication.Encrypt(ticket);

            HttpCookie cookie = new HttpCookie("hanApp", authTicket);

            Response.Cookies.Remove(cookie.Name);
            Response.Cookies.Add(cookie);

            //HttpCookie signCookie = new HttpCookie("hanApp_sign", "1  ");

            //Response.Cookies.Remove(signCookie.Name);
            //Response.Cookies.Add(signCookie);
        }

       4. 调用FormsAuthentication.Encrypt对票据信息进行加密。

       5. 创建HttpCookie,并通过Response.Cookies.Add将cookie发回到前端。

  6. Redirect跳转到登录前的页面。

 

 三. 一般验证:

       1. 对于一般的controller和action,可以有直接使用Authorize Attribute,那么访问他们之前会自动验证是否登录。

       2. 我们也可以自定义一个attribute来实现这个逻辑,只要这个attribute继承AuthorizeAttribute, 或者自己继承ActionFilterAttribute并重写OnActionExecuting方法,即可。

       3. 我们可以在全局增加验证操作,就不需要在每个controller上增加这个attribute. 如下:

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new AuthorizeAttribute());
            filters.Add(new ActionExceptionAttribute());
        }

 

       3. 对于部分不需要进行 验证的action,可以使用AllowAnonymous attribute。

[AllowAnonymous]
public ActionResult Login(s)
{
.....
return View();
}

 

 

Claims-based(基于声明)的身份认证,

先来看一下代码,直观感受一下,如何使用Claims-based(基于声明)的身份认证:

private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
}
 
private async Task SignInAsync()
{
// 1. 利用ASP.NET Identity获取用户对象
var user = await UserManager.FindAsync("UserName", "Password");
// 2. 利用ASP.NET Identity获取identity 对象
ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
// 3. 结合OWIN,引用  Microsoft.Owin.Security命名空间,实现IIS与web应用的解耦
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);
} 

下面这篇文章介绍了如何使用第三方的应用进进登录:http://www.cnblogs.com/clark159/p/5195673.html?utm_source=tuicool&utm_medium=referral

转载于:https://www.cnblogs.com/hankuikui/p/7072697.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值