Form Authentication
1.创建登陆的控制器和视图,实现登陆基本功能
2.创建视图模型,并在Action里面引用。
3.创建一个接口两个类,那个IUserPricipal接口要实现IPrincipal接口,UserPricipal类需要继承这个接口,然后去初始化IIdentity这个接口,UserPricipalModel类则是返回用户输出的Cookie的数据模型。
具体代码:
两个类一个接口:
interface ICustomPrincipal : IPrincipal
{
UserViewModel User { get; set; }
}
public class CustomPriceipal : ICustomPrincipal
{
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
return false;
}
public CustomPriceipal(string email)
{
this.Identity = new GenericIdentity(email);
}
public bool IsAdmin { get; set; } = false;
public string UserName { get; set; }
public UserViewModel User { get; set; }
}
public class CustomPrincipalSerializeModel
{
public string UserName { get; set; }
public bool IsAdmin { get; set; }
public UserViewModel User { get; set; }
}
Action代码 :
[HttpPost]
public ActionResult Login(UserViewModel model)
{
if (model.Password == "" || model.UserName == "")
{
return View(model);
}
CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializeModel.User = model;
serializeModel.UserName = model.UserName;
string userData = serializer.Serialize(serializeModel);
var ticket = new FormsAuthenticationTicket(2, model.UserName, DateTime.Now, DateTime.Now.AddMinutes(1), false, userData);
string encryptTickey = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptTickey);
Response.Cookies.Remove(cookie.Name);
Response.Cookies.Add(cookie);
return Redirect("/Home/Index");
}
Global文件代码:
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if(cookie!=null)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
if (ticket != null && !string.IsNullOrEmpty(ticket.UserData))
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(ticket.UserData);
CustomPriceipal newUser = new CustomPriceipal(ticket.Name);
newUser.User = serializeModel.User;
newUser.UserName = serializeModel.UserName;
newUser.IsAdmin = serializeModel.IsAdmin;
HttpContext.Current.User = newUser;
}
}
}
Form Authentication的更多相关文章
- ASP.NET MVC:Form Authentication 相关的学习资源
看完此图就懂了 看完下面文章必须精通 Form authentication and authorization in ASP.NET Explained: Forms Authentication ...
- Web验证方式(2)--Form Authentication
Form验证方式并不是HTTP标准,而是在微软ASP.NET Web框架下提供的一种验证方式.其大致流程如下: 在上图的流程中,ASP.NET框架提供了如下支持类:( FormsAuthenticat ...
- Asp.net Web Api 2 FORM Authentication Demo
最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了. Demo 一.FORM Aut ...
- SharePoint 2010 Form Authentication (SQL) based on existing database
SharePoint 2010 表单认证,基于现有数据库的用户信息表 本文主要描写叙述本人配置过程中涉及到的步骤,仅作为參考,不要仅限于此步骤. 另外本文通俗易懂,适合大众口味儿. I. 开启并配置基 ...
- Form authentication(表单认证)问题
前言 最近在做ASP.NET MVC中表单认证时出了一些问题,特此记录. 问题 进行表单认证时,在 PostAuthenticateRequest 事件中从Cookie值中解密票据.如下: prote ...
- 关于ASP.NET MVC中Form Authentication与Windows Authentication的简单理解
一般互联网应用,如人人网,微博,都是需要用户登录的,如果用户不登陆,就不能使用此网站.所以,这里都是用FormAuthentication,要求用户填写用户名与密码,然后登录成功后,FormAuthe ...
- 升级framework4.0后form认证票据失效的问题
好久没来了,密码都差点忘了,顺便记录下今天配置环境碰到的小问题 网站使用的form authentication做SSO登录,登录域名使用的framework20配置环境 一个栏目升级为4.0环境后, ...
- [转]OData and Authentication – Part 5 – Custom HttpModules
本文转自:https://blogs.msdn.microsoft.com/odatateam/2010/07/19/odata-and-authentication-part-5-custom-ht ...
- rest-assured之认证授权(Authentication)
rest-assured支持多种认证授权方案,比如:OAuth.digest(摘要认证).certificate(证书认证).form(表单认证)以及preemptive(抢占式基础认证)等.我们可以 ...
随机推荐
- 最详尽的IntelliJ IDEA项目web项目搭建!!!!!!
一.创建一个web项目(首次创建最麻烦) 1.保证安装好软件 2.双击打开软件-->新建一个项目 3.web项目选择如图,先建立一个空的项目空间来放置你的项目,这是一个区别 相当于myeclip ...
- Spring框架总结(九)
三.AOP编程 关注点代码:除了业务代码以外的代码.比如开启事务,关闭事务,异常处理核心业务代码:保存用户这一句才是重点.例子如下:// 保存一个用户public void add(User user ...
- 《深入理解Elasticsearch》README
书目 <深入理解ElasticSearch>拉斐尔·酷奇,马雷克·罗戈任斯基[著]张世武,余洪森,商旦[译] 机械工业出版社,2016.1 本系列包括以下8篇笔记 第01章 Elastic ...
- 第二章第一个项目——关于mime
一句话就能解释清楚. MIME标注HTTP响应类型. 而后缀名标注文件类型. ---------分割线-------- http响应实质上只有数据,没有文件名. 举个例子吧. HTTP/1.1 200 ...
- linux bash命令行基本操作
shell shell 我们叫做壳,我们知道操作系统底层是有一个内核kernel的,内核用来实现所有上层服务,所有上层命令,上层应用所需要的一些基本功能,比如说网络连接,网络通信,比如说键盘驱动, ...
- Delphi XE8 iOS与Android移动应用开发(APP开发)教程[完整中文版]
https://item.taobao.com/item.htm?id=536584650957&toSite=main
- 20145233《网络对抗》Exp5 MSF基础应用
20145233<网络对抗>Exp5 MSF基础应用 实验问题思考 什么是exploit,payload,encode exploit是发现了的可以利用的安全漏洞或者配置弱点,这类模块存储 ...
- VS2010下安装OpenCV2.4.3
本文记录Windows 7 X86 SP1操作系统环境下,安装与配置OpenCV2.4.3的详细步骤.前置需求:已安装有VS2010. 下载并安装OpenCV 从http://www.opencv.o ...
- C#串口数据互通小程序
主要功能: 所编写的程序需将串口1.串口2数据互通,即:串口1接收到数据的同时将数据通过串口2发出,串口2接收到数据的同时将数据通过串口1发出. 并根据需要由指定串口发送或获取数据. 代码如下: us ...
- 如何使用jQuery写一个jQuery插件
jQuery插件其实是前端框架的思维,构成一个框架,个人认为必须满足以下几个基础条件:1. 可重用,2. 兼容性,3. 维护方便,虽说现在有很多比较成熟的前端框架,但是也有部分存在配置麻烦,学习成本大 ...