一、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. Visual Studio express

    之前一直没用过Visual Studio的express版本.在最近一段时间,使用VS2010和2012的破解版都有点问题.vs2010突然不能使用,需要重新安装,家里和单位两台电脑都如此.家里一台电 ...

  2. JavsScript的基本特点

    1.简单性Javascript是一种脚本语言,它采用小程序段的方式实现编程它同样也是一种解释性语言.2.动态性Javascript是动态的,它可以直接对用户或者客户输入做出相应,无须经过Web服务程序 ...

  3. 给Qt生成的exe执行程序添加版本信息

    Windows下的.exe可执行文件的属性中有版本这个信息,含有版本信息.描述.版权等.对于qt程序,要含有这样的信息,那就请如下操作:新建<工程名>.rc文件,在rc文件填入下的信息: ...

  4. http://wiki.apache.org/tomcat/HowTo

    http://wiki.apache.org/tomcat/HowTo Contents Meta How do I add a question to this page? How do I con ...

  5. Delphi 调用系统中的计算器、记事本、画图软件方法

    1.直接调用    前面uses加 ShellAPI ShellExecute(Handle, 'open', PChar('calc.exe'), nil, nil, SW_SHOW); 2.直接调 ...

  6. SAE Python使用经验 好文推荐

    SAE Python使用经验 好文推荐 SAE Python使用经验 好文推荐

  7. MySQL查询当前数据上一条和下一条的记录

    如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): ) [and other_cond ...

  8. python3-day6(模块)

    一.OS模块 1.os.system('ls -l') #子shell运行,获取返回值,不是结果. 2.os.popen('ls -l').read() #获取结果. 二.sys模块 1.sys.ar ...

  9. maven java.lang.OutOfMemoryError:PermGEn space

    配置环境变量: JAVA_OPTS-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m   MAVEN_OPTS-Xms256m -Xmx ...

  10. PhoneGap 和 PhoneGap Build 是什么?

    PhoneGap是目前唯一支持7种平台的开源移动开发框架,支持的平台包括iOS.Android.BlackBerry OS.Palm WebOS.Windows Phone 7.Symbian和Bad ...