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的更多相关文章

  1. ASP.NET MVC:Form Authentication 相关的学习资源

    看完此图就懂了 看完下面文章必须精通 Form authentication and authorization in ASP.NET Explained: Forms Authentication ...

  2. Web验证方式(2)--Form Authentication

    Form验证方式并不是HTTP标准,而是在微软ASP.NET Web框架下提供的一种验证方式.其大致流程如下: 在上图的流程中,ASP.NET框架提供了如下支持类:( FormsAuthenticat ...

  3. Asp.net Web Api 2 FORM Authentication Demo

    最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了. Demo 一.FORM Aut ...

  4. SharePoint 2010 Form Authentication (SQL) based on existing database

    SharePoint 2010 表单认证,基于现有数据库的用户信息表 本文主要描写叙述本人配置过程中涉及到的步骤,仅作为參考,不要仅限于此步骤. 另外本文通俗易懂,适合大众口味儿. I. 开启并配置基 ...

  5. Form authentication(表单认证)问题

    前言 最近在做ASP.NET MVC中表单认证时出了一些问题,特此记录. 问题 进行表单认证时,在 PostAuthenticateRequest 事件中从Cookie值中解密票据.如下: prote ...

  6. 关于ASP.NET MVC中Form Authentication与Windows Authentication的简单理解

    一般互联网应用,如人人网,微博,都是需要用户登录的,如果用户不登陆,就不能使用此网站.所以,这里都是用FormAuthentication,要求用户填写用户名与密码,然后登录成功后,FormAuthe ...

  7. 升级framework4.0后form认证票据失效的问题

    好久没来了,密码都差点忘了,顺便记录下今天配置环境碰到的小问题 网站使用的form authentication做SSO登录,登录域名使用的framework20配置环境 一个栏目升级为4.0环境后, ...

  8. [转]OData and Authentication – Part 5 – Custom HttpModules

    本文转自:https://blogs.msdn.microsoft.com/odatateam/2010/07/19/odata-and-authentication-part-5-custom-ht ...

  9. rest-assured之认证授权(Authentication)

    rest-assured支持多种认证授权方案,比如:OAuth.digest(摘要认证).certificate(证书认证).form(表单认证)以及preemptive(抢占式基础认证)等.我们可以 ...

随机推荐

  1. java 泛型详解(转)

    普通泛型 class Point<T>{       // 此处可以随便写标识符号,T是type的简称 private T var ; // var的类型由T指定,即:由外部指定 publ ...

  2. SSH整合案例

    1.Hibernate框架 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernat ...

  3. JS 单例模式

    <parctical common lisp>的作者曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通用的解决方案. 不管是弱类型 ...

  4. Statement 接口的应用(存在sql语句的注入风险)

    实现简单的登录功能 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; impo ...

  5. WSAGetOverlappedResult函数

    WSAGetOverlappedResult函数 通过WSAWaitForMultipleEvents函数来得到重叠操作完成的通知,那么自然也需要一个函数来查询一下重叠操作的结果,定义如下 BOOL ...

  6. Python之算法基础

    1>递归相关: 递归:递归算法是一种直接或间接地调用自身算法的过程,在计算机编写程序中,递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且                   易于 ...

  7. JavaScript - this详解 (三)

    闭包 this 执行上下文决定了变量作用域 而闭包,它其实是一种决策,是一种模式,让我们可以灵活的改变变量作用域. 按惯例,上栗子 var global = 'global';function out ...

  8. memcached整理の实践

    对于memcached使用内存来存取数据,一般情况下,速度比直接从数据库或者文件系统存取要快,memcached最常用的场景是利用其“存取快”来保护数据库,防止高频率存取数据库. 缓存数据库查询结果 ...

  9. Inno Setup 通用脚本及简要说明( 一般情况够用了)

    ;以下脚本主要完成创建开始菜单和桌面的快捷方式,目录安装. #define MyAppName "我的软件名" #define MyAppVersion "1.0&quo ...

  10. mybatis mybatis.xml 文件和properties文件结合来进行配置数据源