一、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. 第一个微信小程序(实现点击一个按钮弹出toast)

    今天根据网上的教程搭建了微信小程序的环境,然后看文档做了一个简单的小应用. 项目的目录是这个样子的: app.js.app.json.app.wxss是全局文件,必不可少的文件.定义在app.wxss ...

  2. swift中的&-备

    参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以引用方式传递,这也是可以实现的,Swift提供 ...

  3. documentElement vs body区别

    documentElement.scrollTop------>0因为,他包含head, body body.scrollTop------------------>才是正确的 scrol ...

  4. poj2068--Nim

    题意:给你2n个人,两方各n个人,交叉坐,每个人可以取的石子有一个最大限制,总共有S颗石子,哪一方取了最后一颗石子就输了,问先取石子的这一方是否有必胜策略. DP,dp[i][j]代表第i个人还有J个 ...

  5. Qt调用VC++生成的动态链接库

    Qt如何调用VC++生成的动态链接库?假设当前有VC++编译器生成的动态库文件testdll.h,testdll.lib和testdll.dll. testdll.h文件源码如下: #ifdef TE ...

  6. quote, quasiquote, unquote和unquote-splicing

    关于符号类型 符号类型又称引用类型,在概要一文中本人介绍得非常的模糊,使很多初学者不理解.符号类型在Scheme语言中是最基础也是最重要的一种类型,这是因为Scheme语言的祖先Lisp语言的最初目的 ...

  7. op+3g

    https://forum.openwrt.org/viewtopic.php?id=44895 http://eko.one.pl/forum/viewtopic.php?id=10269 http ...

  8. openstack组件手动部署整合

    preface:当你完全且正确的配置好整个OpenStack ENV 你将能看到的和体验到的!!! 我们先来看看简单效果吧,祝君能在这条路上走的更远,更好;

  9. 什么是 docker?

    关于 Docker 是什么,有个著名的隐喻:集装箱.但是它却起了个“码头工人”( docker 的英文翻译)的名字.这无疑给使用者很多暗示:“快来用吧!用了 Docker ,就像世界出现了集装箱,这样 ...

  10. poj 2441 Arrange the Bulls(状态压缩dp)

    Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to ...