asp.net登录验证FormsAuthenticationTicket和FormsAuthentication类
登录部分使用的类
FormsAuthentication 为 Web 应用程序管理 Forms 身份验证服务。
配置启用身份验证,WEB.config配置:
<system.web>
<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
属性: FormsCookieName,用于存储 Forms 身份验证票证的 Cookie 名称。 默认值是“.ASPXAUTH”。
下面的代码示例设置FormsCookieName属性值,使用Web.config 文件中的name属性。
<authentication mode="Forms">
<forms loginUrl="member_login.aspx"
cookieless="UseCookies"
name=".ASPXFORMSAUTH" />
</authentication>
FormsCookieName使用 ASP.NET 应用程序配置文件name属性设置属性值。
FormsCookieName用于引用存储的 cookieFormsAuthenticationTicket信息。
FormsAuthenticationTicket 提供对票证的属性和值的访问,这些票证用于 Forms 身份验证对用户进行标识。
FormsAuthenticationTicket类用于创建一个对象,表示窗体身份验证用于标识身份验证的用户的身份验证票证。
private void Login_Click(Object sender, EventArgs e)
{
// Create a custom FormsAuthenticationTicket containing
// application specific data for the user. string username = UserNameTextBox.Text;
string password = UserPassTextBox.Text;
bool isPersistent = false; if (Membership.ValidateUser(username, password))
{
string userData = "ApplicationSpecific data for this user."; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(,
username,
DateTime.Now,
DateTime.Now.AddMinutes(),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath); // Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket); // Create the cookie.
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); // Redirect back to original URL.
Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));
}
else
{
Msg.Text = "Login failed. Please check your user name and password and try again.";
}
}
验证部分
FormsIdentity和FormsAuthenticationTicket
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = App.Context.Request.Cookies[cookieName];
if (null == authCookie)
{
// 沒有驗證 Cookie。
return;
}
if (authCookie.Value == null)
{
// 沒有驗證 Cookie。
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch (Exception ex)
{
// 記錄例外狀況詳細資料 (為簡單起見已省略)
FileTxtLogs.WriteLog(ex.ToString());
return;
} if (null == authTicket)
{
// Cookie 無法解密。
return;
} // 建立 Identity 物件
FormsIdentity id = new FormsIdentity(authTicket);
App.Context.User = new PermissionPrincipal(id);
IsAuthenticated 获取一个值,该值指示是否进行身份验证。
文章:How to: Implement Simple Forms Authentication
asp.net登录验证FormsAuthenticationTicket和FormsAuthentication类的更多相关文章
- ASP.NET底层封装HttpModule实例---FormsAuthentication类的分析
HttpModule是用来注册HttpApplication事件的,实现IHttpModule接口的托管代码模块可以访问该请求管道的所有事件.那么对于我们最常用的ASP.NET Forms身份验证模块 ...
- C# asp.net 抓取需要登录的网页内容 抓取asp.net登录验证的网站
private void btnASPNET_Click(object sender, EventArgs e) { Dictionary<string, s ...
- ASP.NET登录验证
protected void btnLogin_Click(object sender, EventArgs e) { string username = txtUserName.Value.Trim ...
- ASP.NET 登录验证 ihttpmoudle
问题: 1.iis版本不同(IIS7.0,应用程序池采用的是集成模式,换成经典模式才起作用.) 在 IIS 7 以下的版本中,应用以下配置: <system.web> <httpMo ...
- asp.net登录状态验证
文章:ASP.NET 登录验证 文章:ASP.NET MVC下判断用户登录和授权状态方法 文章:.net学习笔记---HttpHandle与HttpModule 第一篇文章,介绍了 1)早期的Base ...
- ASP.NET MVC 登录验证
好久没写随笔了,这段时间没 什么事情,领导 一直没安排任务,索性 一直在研究代码,说实在的,这个登录都 搞得我云里雾里的,所以这次我可能也讲得不是 特别清楚,但是 我尽力把我知道的讲出来,顺便也对自 ...
- ASP.NET MVC4 Forms 登录验证
Web.config配置: 在<system.web>节下: <authentication mode="Forms"> <forms loginUr ...
- 解決 IE10 浏览器无法使用 ASP.NET From 验证登录的问题
最近应项目用到ASP.Net表单验证机制(FormsAuthentication),来判断用户是否已经登录,一切测试顺利,最后发布到IIS中后在IE10测试是发现始终判断用户没登录(其他浏览器一切正常 ...
- Asp.NetMVC利用LigerUI搭建一个简单的后台管理详解(函登录验证)
上一篇 Asp.Net 中Grid详解两种方法使用LigerUI加载数据库数据填充数据分页 了解了LigerUI 中Grid的基本用法 现在结合上一篇的内容做一个简单的后台管理,当然也有前台的页面 ...
随机推荐
- Linux服务-http
目录 1. httpd简介 2. httpd版本 2.1 httpd的特性 2.2 httpd-2.4新增的模块 3. httpd基础 3.1 httpd自带的工具程序 3.2 rpm包安装的http ...
- c++ 条件判断
if语句 认识算术比较 运算符表达式用法 关系运算符表达式 一. 基本的if语句 if (条件成立) { 则执行此语句 }; // ) printf("条件成立"); 二.认识算术 ...
- Kubernetes学习之路(四)之Node节点二进制部署
K8S Node节点部署 1.部署kubelet (1)二进制包准备 [root@linux-node1 ~]# cd /usr/local/src/kubernetes/server/bin/ [r ...
- Python之subprocess模块、sys模块
一.subprocess模块 # import os # os.system('tasklist') #类似cmd输入系统命令 ''' subprocess的目的就是启动一个新的进程并且与之通信. s ...
- mongodb原生node驱动
写在前面 最近读<node.js学习指南>,对于mongodb没有介绍太多的工作原理,但是对于一个前端开发者,即使你还没有用过这种数据库也可以让你很好的理解和使用 一本非常好的 ...
- [原]Asp.net Core 2.1.2 测试成功Ajax上传文件新解法
利用layui框架可以上传文件调试拦截成功! [HttpPost] public IActionResult Method1(IFormFile file) { return Json(new{suc ...
- WHO ARE YOU?--writeup
TIPS:广东强网杯线上题 总结知识点:BASE64,ROT13 0x00 Base64 什么是Base64? Base64编码原理 其用途 什么是Base64? Base64是一种基于64个可打印字 ...
- java两年工作经验有什么经验
这两年里,了解了完整项目的开发过程,知道如何快速入手一个完全没接触过的项目:譬如先了解数据库关系后,马上熟悉一个功能从前端到后端的实现过程,自己再写一个功 能,这样子就能马上上手开发项目,之后在慢慢了 ...
- 基于Vue+Spring MVC+MyBatis+Shiro+Dubbo开发的分布式后台管理系统
本文项目代码: 服务端:https://github.com/lining90567/dubbo-demo-server 前端:https://github.com/lining90567/dubbo ...
- halcon算子之tuple_gen_const,用于生成特定长度的元组并且初始化其元素
原文地址:http://blog.sina.com.cn/s/blog_d38f8be50102wczk.html 函数原型: tuple_gen_const(: : Length, Const : ...