一、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堆栈的理解

    对ARM堆栈的理解 堆栈严格来说应该叫做栈,栈(Stack)是限定仅在一端进行插入或删除操作的线性表.因此,对栈来说,可以进行插入或删除操作的一端端称为栈顶(top),相应地,另一端称为栈底(bott ...

  2. Scala学习笔记--Akka

    待完成 http://www.gtan.com/akka_doc/ http://my.oschina.net/mingdong/blog/297972 http://www.jdon.com/con ...

  3. sqlserver查询分析器创建sql查询日志

    USE [master]GO /****** Object: Audit [ServerAuditDB] Script Date: 2016-04-28 16:02:58 ******/CREATE ...

  4. 怎样制作百度recovery【转】

    由于recovery的硬件相关性比较强,使得recovery的通用性不强,项目组为了降低整个开发的难度,coron项目里面默认是编译生成百度recovery的. 不过还是有很多开发者问私下我,怎样制作 ...

  5. ROM包内的大致框架及各个文件的作用[转]

    1.system/app这个里面主要存放的是常规的应用程序,都是以apk格式结尾的文件,在这个文件夹下面的程序为系统默认的组件,个人安装的软件不会出现在这里,而是data文件夹中. 2.system/ ...

  6. Android 不通过parent实现样式继承

    1:styles.xml文件内容如下: <!-- TextView Style --> <style name="tvStyle"> <item na ...

  7. android资料

    http://bbs.51cto.com/thread-903936-1.html http://zhidao.baidu.com/question/195697097.html?sort=4& ...

  8. WPF中的换行符

    原文:WPF中的换行符 WPF中UI上和后台代码中的换行符不同. 其中: XAML中为 C#代码中为 \r\n 或者: Environment.NewLine 版权声明:本文为博主原创文章,未经博主允 ...

  9. Struts2配置RESULT中TYPE的参数说明

    chain           用来处理Action链,被跳转的action中仍能获取上个页面的值,如request信息.           com.opensymphony.xwork2.Acti ...

  10. 黑马程序员_Java面向对象_包

    7.包 7.1包(package) 对类文件进行分类管理. 给类提供多层命名空间. 写在程序文件的第一行. 类名的全称是:包名.类名. 包也是一种封装形式. 利用命令行自动生成文件夹格式:D:\jav ...