博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET内置票据认证
阅读量:7011 次
发布时间:2019-06-28

本文共 3475 字,大约阅读时间需要 11 分钟。

1、 在根目录建立一个Global.asax文件,烤入一段代码

1     protected void Application_AuthenticateRequest(object SENDER, EventArgs e)  2     {
3 if (HttpContext.Current.User != null) 4 {
5 if (HttpContext.Current.User.Identity.IsAuthenticated) 6 {
7 if (HttpContext.Current.User.Identity is FormsIdentity) 8 {
9 FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; 10 FormsAuthenticationTicket tiecket = id.Ticket; 11 string userData = tiecket.UserData; 12 string[] roles = userData.Split(','); 13 HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id, roles); 14 } 15 } 16 } 17 }

2、在web.config 文件中配置目录权限及登录页

登录页,在system.web节点中

1 
2
3

配置目录权限,在system.web节点外面

1 
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

3、在登录页的登录事件中的登录成功后烤入一段代码

// string roles = "admin"; 代表用户角色 新添加  string roles = "admin";  HttpCookie cook; string strReturnURL;  FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, user, DateTime.Now, DateTime.Now.AddMinutes(30), false, roles);  cook = new HttpCookie("mycook");  cook.Value = FormsAuthentication.Encrypt(ticket);  Response.Cookies.Add(cook);  strReturnURL = Request.Params["ReturnUrl"]; if (strReturnURL != null && strReturnURL.Contains(".aspx"))  {
Response.Redirect(strReturnURL); } else {
Session["已经登录"] = true; Response.Redirect("index.aspx"); }

后台页面调用登录的用户名实例:

litname.Text= User.Identity.Name.ToString();

这样基本上就可以了

但是有个疑问 如果是多用户系统,用户没有登录就跳转到用户的登录页怎么办呢?

刚上面的办法是没办法跳转到2个登录页面的 这时候我们就需要建立一个中间的跳转登录页来根据ReturnURL中是否包含

admin 或者user来判断跳转到哪个登录页面了

建立 login_redirect.aspx

1 using System;  2  using System.Collections.Generic;  3  using System.Linq;  4  using System.Web;  5  using System.Web.UI;  6  using System.Web.UI.WebControls;  7    8 namespace xh.shop.web  9  {
10 public partial class login_redirect : System.Web.UI.Page 11 {
12 protected void Page_Load(object sender, EventArgs e) 13 {
14 string strReturnURL = Request.Params["ReturnUrl"]; 15 if (strReturnURL != null && strReturnURL.Contains("admin")) 16 17 //包含的字段 18 {
19 Response.Redirect("admin/login.aspx?ReturnUrl=" + strReturnURL); 20 21 //如果包含admin则跳转到否则跳转到*** 22 } 23 else 24 { Response.Redirect("index.aspx?ReturnUrl=" + strReturnURL);} 25 26 } 27 } 28 }

最后config里面的loginurl改成 login_redirect.aspx就可以了

1 
2
3

正文补充知识:

可以用登录控件直接显示登录状态 登录名等

没有登录显示的样式
登录后显示的样式
你好!

注销函数

//首先引入using System.Web.Security;protectedvoid loginout(object sender, EventArgs e) {
FormsAuthentication.SignOut(); //注销当前登录用户}

转载地址:http://fmjtl.baihongyu.com/

你可能感兴趣的文章
人工智能标配语言Python纳入2018高考科目!
查看>>
常用命令
查看>>
活动目录物理结构详解
查看>>
Change column color for columnChart in extjs
查看>>
linux基本练习题
查看>>
PHP典型模块与项目实战大全 源码分享
查看>>
我的友情链接
查看>>
模拟实现strcat
查看>>
实现strncpy
查看>>
C语言矩阵加法乘法
查看>>
apache和tomcat7.0整合
查看>>
微软发布Office 2010 Service Pack 2 提升安全性和兼容性
查看>>
apt-get安装tomcat并测试nginx负载均衡
查看>>
写作_01-02_templatev1.0
查看>>
Corosync+Pacemaker+Ldirectord+Lvs+Httpd
查看>>
Web前端笔面试
查看>>
我的友情链接
查看>>
The type org.apache.commons.lang.exception.NestableRuntimeException cannot be resolved.
查看>>
在Ext JS 6中添加本地化包
查看>>
PHP程序员的简单运维
查看>>