.net mvc结合微软提供的FormsAuthenticationTicket登陆
一、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登陆的更多相关文章
- MVC4 AspNet MVC下的Ajax / 使用微软提供的Ajax请求脚本 [jquery.unobtrusive-ajax.min.js]
源码参考:链接:http://pan.baidu.com/s/1pKhHHMj 密码:mkr4 1:新建-->项目-->Web-->ASP.NET MVC 4 Web 应用程序.命 ...
- 使用UI Automation实现自动化测试 --微软提供的控件Pattern
微软提供的控件Pattern System.Windows.Automation 命名空间 System.Windows.Automation.BasePattern 为控件模式类提供基实现 Syst ...
- 微软提供的API的各个版本之间的区别
First Floor Software这个diff lists非常方便的给出了微软提供的API的各个版本之间的区别,比如下表是.NET 4和.NET 4.5的API变化总结.我们可以看到.NET 4 ...
- 微软提供了三个核心服务:Windows+Office 365+Azure
微软提供了三个核心服务:Windows+Office 365+Azure 英语新闻来源:http://techcrunch.com/2014/11/10/microsofts-ceo-breaks-d ...
- iis重写模块实现程序自动二级域名,微软提供的URL重写2.0版本适用IIS以上
在iis7以后微软提供了url重写2.0版本,可以通过安装重写组件来实现.适用于iis7以上版本. 安装有两种方式可以选择,一是下载安装文件,二是通过“web平台安装程序”安装 1.下载安装文件 下载 ...
- 借助微软提供的url重写类库URLRewriter.dll(1.0)实现程序自动二级域名,域名需要泛解析
二级域名和系统中会员帐号自动关联,也就是系统中注册一个会员,会员自动就可以通过二级域名来访问,比如我的帐号是zhangsan,我在morecoder.com注册后,访问zhangsan.morecod ...
- 使用微软提供的Office Online实现Office文档的在线查看,编辑等功能
使用微软提供的Office Online平台只需要一个网址即可在线查看Xls,doc,PPT等文档 http://view.officeapps.live.com/op/view.aspx?src=要 ...
- 虚拟化(四):vsphere高可用功能前提-共享存储搭建(使用微软提供的iscsi software target,也可以使用免费开源的openfiler)
虚拟化(一):虚拟化及vmware产品介绍 虚拟化(二):虚拟化及vmware workstation产品使用 虚拟化(三):vsphere套件的安装注意及使用 虚拟化(四):vsphere高可用功能 ...
- 如何使用微软提供的TCHAR.H头文件?
转载:https://www.cnblogs.com/flyingspark/archive/2012/03/16/2399788.html 如何使用微软提供的TCHAR.H头文件? 如果你现在写的代 ...
随机推荐
- SolrCloud初识
文章摘自:http://www.bubuko.com/infodetail-923588.html 一.概述 Lucene是一个Java语言编写的利用倒排原理实现的文本检索类库: Solr是以Luce ...
- 关于MPLS协议几个容易忽略的点
1.随着硬件技术的进步,产生了高速路由器和三层交换机,MPLS提高转发的速度的初衷已经没有多少意义.但是MPLS由于其支持标签栈和面向连接的特点,使其在虚拟专用网,流量工程,QoS等方面得到广泛的应用 ...
- dom4j解析xml字符串
import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.Docume ...
- 第8章 Android数据存储与IO——File存储
openFileOutput/openFileInput 这是android自带的两种解决方案.
- xdebug及webgrind的联用
参考URL: http://www.tuicool.com/articles/ERFNva http://blog.sina.com.cn/s/blog_635833b3010127q5.html h ...
- jQuery 弹出div层
目的:使用jQuery弹出一个div窗口:这种效果经常应用于页面内容的展示,登录效果的实现.其实,实现这种效果有好多种方式: 效果如下: 代码如下: <html> <head> ...
- QListWidget方式显示缩略图
最近在工作中经常遇到了一个问题就是把把文件夹中的图片全部以缩略图的形式显示出来,刚开始的时候一头雾水,不知道怎么办,经过在网上查资料,发现QListWidget控件可以实现图片的缩略图显示,但是不知道 ...
- python在一个列表中查找
# -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#简化一些操作#1:在一个列表中查找""" ...
- 如何将XML转换成XSD(XML Schema)文件
将xml装换为xsd,先决条件是已经安装了Visual Stutio 1) 输入cmd在运行窗口 2) 将xsd的路径加入到path变量 set path=%path%;C:\Program File ...
- hdu 4405 Aeroplane chess (概率DP)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...