ASP.NETnbsp;内置票据认证

2024-08-28 11:48
文章标签 认证 内置 asp 票据 netnbsp

本文主要是介绍ASP.NETnbsp;内置票据认证,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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 <</span>authentication mode="Forms">
2 <</span>forms name="mycook" loginUrl="login.aspx" protection="All" path="/"/>
3 </</span>authentication>

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

复制代码
 1 <</span>location path="admin">
2 <</span>system.web>
3 <</span>authorization>
4 <</span>allow roles="admin"/>
5 <</span>deny users="*"/>
6 </</span>authorization>
7 </</span>system.web>
8 </</span>location>
9 <</span>location path="user">
10 <</span>system.web>
11 <</span>authorization>
12 <</span>allow roles="user"/>
13 <</span>deny users="*"/>
14 </</span>authorization>
15 </</span>system.web>
16 </</span>location>
17 <</span>location path="admin/admin_login.aspx">
18 <</span>system.web>
19 <</span>authorization>
20 <</span>allow users="*"/>
21 </</span>authorization>
22 </</span>system.web>
23 </</span>location>
24 <</span>location path="user/user_login.aspx">
25 <</span>system.web>
26 <</span>authorization>
27 <</span>allow users="*"/>
28 </</span>authorization>
29 </</span>system.web>
30 </</span>location>
复制代码

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 "Forms">
2 "mycook" loginUrl="login.aspx" protection="All" path="/"/>
3

正文补充知识:

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

<</span>asp:LoginViewID="LoginView1" runat="server"><</span>AnonymousTemplate> 没有登录显示的样式 </</span>AnonymousTemplate><</span>LoggedInTemplate> 登录后显示的样式 <</span>br /><</span>br /><</span>br/><</span>br /> 你好! <</span>asp:LoginNameID="LoginName1" runat="server"/><</span>asp:LoginStatusID="LoginStatus1" runat="server"/></</span>LoggedInTemplate></</span>asp:LoginView>

注销函数

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

这篇关于ASP.NETnbsp;内置票据认证的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1114685

相关文章

springboot依靠security实现digest认证的实践

《springboot依靠security实现digest认证的实践》HTTP摘要认证通过加密参数(如nonce、response)验证身份,避免明文传输,但存在密码存储风险,相比基本认证更安全,却因... 目录概述参数Demopom.XML依赖Digest1Application.JavaMyPasswo

Nginx添加内置模块过程

《Nginx添加内置模块过程》文章指导如何检查并添加Nginx的with-http_gzip_static模块:确认该模块未默认安装后,需下载同版本源码重新编译,备份替换原有二进制文件,最后重启服务验... 目录1、查看Nginx已编辑的模块2、Nginx官网查看内置模块3、停止Nginx服务4、Nginx

在ASP.NET项目中如何使用C#生成二维码

《在ASP.NET项目中如何使用C#生成二维码》二维码(QRCode)已广泛应用于网址分享,支付链接等场景,本文将以ASP.NET为示例,演示如何实现输入文本/URL,生成二维码,在线显示与下载的完整... 目录创建前端页面(Index.cshtml)后端二维码生成逻辑(Index.cshtml.cs)总结

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

SpringSecurity6.0 如何通过JWTtoken进行认证授权

《SpringSecurity6.0如何通过JWTtoken进行认证授权》:本文主要介绍SpringSecurity6.0通过JWTtoken进行认证授权的过程,本文给大家介绍的非常详细,感兴趣... 目录项目依赖认证UserDetailService生成JWT token权限控制小结之前写过一个文章,从S

springboot security使用jwt认证方式

《springbootsecurity使用jwt认证方式》:本文主要介绍springbootsecurity使用jwt认证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录前言代码示例依赖定义mapper定义用户信息的实体beansecurity相关的类提供登录接口测试提供一