一、Web.config

  <system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="/Sign/SignIn" defaultUrl="/Home/Index" />
</authentication>
</system.web>

二、SignController(主要实现)

    /// <summary>
/// 登陆、注销功能
/// </summary>
public class SignController : Controller
{
/// <summary>
/// 登陆页面
/// </summary>
/// <returns></returns>
[AllowAnonymous]
public ActionResult SignIn()
{
var isAuthenticated = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
if (isAuthenticated) //已经验证用户
{
return Redirect(FormsAuthentication.DefaultUrl);
}
var reUrl = FormsAuthentication.GetRedirectUrl(HttpContext.User.Identity.Name, false);
ViewBag.RedirectUrl = reUrl; return View();
} /// <summary>
/// 登陆功能
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="pwd">密码</param>
/// <returns></returns>
[HttpPost]
[AllowAnonymous]
public JsonResult LogIn(string userName, string pwd)
{
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(pwd))
{
var ticket = new FormsAuthenticationTicket(, userName, DateTime.Now, DateTime.Now.AddMinutes(), false, userName + pwd);
string hashTicket = FormsAuthentication.Encrypt(ticket);
var userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
HttpContext.Response.Cookies.Add(userCookie); //添加cookies var identity = new FormsIdentity(ticket);
HttpContext.User = new CustomPrincipal(identity); //获取请求信息,通过自定义标志(重点) return Json();
}
else
return Json();
} /// <summary>
/// 注销
/// </summary>
/// <returns></returns>
[AllowAnonymous]
public ActionResult SignOut()
{
FormsAuthentication.SignOut();
return Redirect(FormsAuthentication.LoginUrl);
}
}

model

    /// <summary>
/// 定义用户对象的基本功能(自定义)
/// </summary>
public class CustomPrincipal : IPrincipal
{
#region 字段
private IIdentity _identity;
#endregion #region 属性
public IIdentity Identity
{
get { return _identity; }
}
#endregion #region 构造函数
public CustomPrincipal(IIdentity identity)
{
_identity = identity;
}
#endregion #region 方法
public bool IsInRole(string role)
{
throw new NotImplementedException();
}
#endregion
}

view

@{
ViewBag.Title = "SignIn";
} <h2>SignIn</h2>
<a href="@Url.Action("Index", "Home")">主页</a>
<a href="@Url.Action("SignIn", "Sign")"> 登陆</a>
<a href="@Url.Action("SignOut", "Sign")"> 注销</a>
<a href="@Url.Action("Detail", "Home")"> 详细</a>
<h2>name:@HttpContext.Current.User.Identity.Name</h2>
<br />
<div>用户名:<input id="user_name" /></div>
<div>密 码:<input id="user_pwd" /></div>
<div><input id="btnSumbit" type="button" value="提交" /></div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
$(function () {
$('#btnSumbit').click(function () {
var data = {
userName: $('#user_name').val(),
pwd: $('#user_pwd').val()
}
$.post('@Url.Action("LogIn", "Sign")', data, function (result) {
if (result == )
location.href = '@ViewBag.RedirectUrl';
else alert(result);
});
}) })
</script>

三、需要权限的控制器(调用方式)

    [Authorize]
public class BaseController : Controller
{
}
    public class HomeController : BaseController
{
/// <summary>
/// 首页
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
return View();
} public ActionResult Detail()
{
var cidentity = (FormsIdentity)HttpContext.User.Identity;
var data = cidentity.Ticket.UserData;//获取存储的 数据
var name = HttpContext.User.Identity.Name;
var dd = FormsAuthentication.FormsCookieName;
return View();
} }
@{
ViewBag.Title = "Index";
} <h2>Index</h2>
<a href="@Url.Action("Index", "Home")">主页</a>
<a href="@Url.Action("SignIn", "Sign")"> 登陆</a>
<a href="@Url.Action("SignOut", "Sign")"> 注销</a>
<a href="@Url.Action("Detail", "Home")"> 详细</a>
<h2>name:@HttpContext.Current.User.Identity.Name</h2>
@{
ViewBag.Title = "Detail";
} <h2>Detail</h2>
<a href="@Url.Action("Index", "Home")">主页</a>
<a href="@Url.Action("SignIn", "Sign")"> 登陆</a>
<a href="@Url.Action("SignOut", "Sign")"> 注销</a>
<a href="@Url.Action("Detail", "Home")"> 详细</a>
<h2>name:@HttpContext.Current.User.Identity.Name</h2>

.net mvc结合微软提供的FormsAuthenticationTicket登陆的更多相关文章

  1. MVC4 AspNet MVC下的Ajax / 使用微软提供的Ajax请求脚本 [jquery.unobtrusive-ajax.min.js]

    源码参考:链接:http://pan.baidu.com/s/1pKhHHMj  密码:mkr4 1:新建-->项目-->Web-->ASP.NET MVC 4 Web 应用程序.命 ...

  2. 使用UI Automation实现自动化测试 --微软提供的控件Pattern

    微软提供的控件Pattern System.Windows.Automation 命名空间 System.Windows.Automation.BasePattern 为控件模式类提供基实现 Syst ...

  3. 微软提供的API的各个版本之间的区别

    First Floor Software这个diff lists非常方便的给出了微软提供的API的各个版本之间的区别,比如下表是.NET 4和.NET 4.5的API变化总结.我们可以看到.NET 4 ...

  4. 微软提供了三个核心服务:Windows+Office 365+Azure

    微软提供了三个核心服务:Windows+Office 365+Azure 英语新闻来源:http://techcrunch.com/2014/11/10/microsofts-ceo-breaks-d ...

  5. iis重写模块实现程序自动二级域名,微软提供的URL重写2.0版本适用IIS以上

    在iis7以后微软提供了url重写2.0版本,可以通过安装重写组件来实现.适用于iis7以上版本. 安装有两种方式可以选择,一是下载安装文件,二是通过“web平台安装程序”安装 1.下载安装文件 下载 ...

  6. 借助微软提供的url重写类库URLRewriter.dll(1.0)实现程序自动二级域名,域名需要泛解析

    二级域名和系统中会员帐号自动关联,也就是系统中注册一个会员,会员自动就可以通过二级域名来访问,比如我的帐号是zhangsan,我在morecoder.com注册后,访问zhangsan.morecod ...

  7. 使用微软提供的Office Online实现Office文档的在线查看,编辑等功能

    使用微软提供的Office Online平台只需要一个网址即可在线查看Xls,doc,PPT等文档 http://view.officeapps.live.com/op/view.aspx?src=要 ...

  8. 虚拟化(四):vsphere高可用功能前提-共享存储搭建(使用微软提供的iscsi software target,也可以使用免费开源的openfiler)

    虚拟化(一):虚拟化及vmware产品介绍 虚拟化(二):虚拟化及vmware workstation产品使用 虚拟化(三):vsphere套件的安装注意及使用 虚拟化(四):vsphere高可用功能 ...

  9. 如何使用微软提供的TCHAR.H头文件?

    转载:https://www.cnblogs.com/flyingspark/archive/2012/03/16/2399788.html 如何使用微软提供的TCHAR.H头文件? 如果你现在写的代 ...

随机推荐

  1. 变量数据是怎么进ARM中的RAM中?

    这篇文章 是从网上复制过来的.觉得不错,分享给大家.原文地址:http://eetrend.com/forum/100028828 ARM的体系结构有很多很多介绍的地方,从其7种模式到CPSR状态寄存 ...

  2. DOM commend

    var comment = document.createComment("commend content"); var elem = document.getElementByI ...

  3. CRACKING THE CODING INTERVIEW 笔记(1)

    1. Arrays and Strings 1.1 Hash Tables 哈希表,简单的说就是由一个数组和一个hash函数组成实现key/value映射并且能高效的查找的数据结构.最简单的想法就是将 ...

  4. WCF 基于Cookie的登录验证回传问题的解决

    参考资料: http://www.cnblogs.com/czcz1024/p/3333138.html http://megakemp.com/2009/02/06/managing-shared- ...

  5. C# 二进制,十进制,十六进制

    //十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...

  6. GDB 的MI接口

    背景介绍: libgdb过时了,目前的GDB调试前端都不用libgdb 目前有两种比较流行:- MI接口,现在应该是MI II接口,是Eclipse CDT所采用的方式- emac输出接口,这个似乎有 ...

  7. 【转】win7+ubuntu双系统安装方法--不错

    原文网址:http://blog.csdn.net/lvanneo/article/details/16885121 前段时间又安装一下win7+ubuntu双系统,过段时间就会忘记,这次自己写下来, ...

  8. 黑马程序员_Java面向对象_内部类

    6.面向对象_内部类 1.内部类定义 内部类:将一个类定义在另一个类里面,对里面那个类就称为内部类.(内置类.嵌套类)内部类可以被私有修饰. 2.内部类访问规则 访问特点: 内部类可以直接访问外部类中 ...

  9. class 类(2)

    类属性和实例属性 一个类实例化后,实例是一个对象,有属性.同样,类也是一个对象,它也有属性. >>> class A(object): ... x = 7 ... >>& ...

  10. c#读写cookie

    读 response.SetCokie(new HttpCookie("Color",TextBox1.Text);写 request.Cookies["color&qu ...