用户验证之自定义身份验证
来源:互联网 发布:linux oracle11g卸载 编辑:程序博客网 时间:2024/05/22 02:14
这是今天在课堂上演示的例子代码,自定义身份验证的简单做法是通过GenericPrinciple和GenericIdentity实现,也可以通过自定义Principle和Identity实现。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Security;using System.Security.Principal;namespace _03_CustomAuthentication{ class Program { static void Main(string[] args) { //这里实现自定义验证(GenericPrinciple) //要求用户输入用户名和密码 Console.WriteLine("请输入用户名:"); string username = Console.ReadLine(); Console.WriteLine("请输入密码:"); string password = Console.ReadLine(); //现在的做法是直接硬编码来做身份验证 if (AuthenticateUser(username, password)) { Console.WriteLine("欢迎使用:{0}!", username); //GenericPrincipal p =(GenericPrincipal)Thread.CurrentPrincipal; //Console.WriteLine("当前您是属于管理员:{0}", // p.IsInRole("Admin")); MyPrinciple p = (MyPrinciple)Thread.CurrentPrincipal; MyIdentity i = p.Identity as MyIdentity; Console.WriteLine("当前您是属于管理员:{0}", p.IsInRole("Admin")); Console.WriteLine("用户的角色列表:"); foreach (var item in i.Roles) { Console.WriteLine(item); } } else Console.WriteLine("你不是合法用户"); Console.Read(); } private static bool AuthenticateUser(string username, string password) { if (username == "chenxizhang" && password == "password") { #region GenericIdentity //GenericIdentity identity = new GenericIdentity( // username,"Custom"); //GenericPrincipal principal = new GenericPrincipal( // identity, // new[] { "Admin" }); //Thread.CurrentPrincipal = principal; #endregion MyIdentity identity = new MyIdentity( username, new[] { "Admin" }); MyPrinciple principle = new MyPrinciple( identity, identity.Roles); Thread.CurrentPrincipal = principle; return true; } return false; } } class MyPrinciple : IPrincipal { public MyPrinciple(IIdentity identity, string[] roles) { _identity = identity; _roles = roles; } string[] _roles; private IIdentity _identity; private MyPrinciple() { }//禁用默认构造器 #region IPrincipal 成员 public IIdentity Identity { get { return _identity; } } public bool IsInRole(string role) { return _roles.Contains(role); } #endregion } class MyIdentity : IIdentity { public MyIdentity(string name, string[] roles) { _name = name; _roles = roles; } private string[] _roles; public string[] Roles { get { return _roles; } } private MyIdentity() { } #region IIdentity 成员 public string AuthenticationType { get { return "自定义验证"; } } public bool IsAuthenticated { get { return true; } } private string _name; public string Name { get { return _name; } } #endregion }}.csharpcode, .csharpcode pre{font-size: small;color: black;font-family: consolas, "Courier New", courier, monospace;background-color: #ffffff;/*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {background-color: #f4f4f4;width: 100%;margin: 0em;}.csharpcode .lnum { color: #606060; }
- 用户验证之自定义身份验证
- 用户身份验证之Windows验证
- 身份验证之绝对验证
- 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇
- 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇
- 使用HttpContext中的User属性来实现用户身份验证之用户验证票篇[转载]
- 用户验证心得--基于FORMS的身份验证
- phpmyadmin 身份验证(用户验证)设置
- 用户身份验证
- 用户身份验证
- Windows IIS 7 用户验证中默认缺少 Windows 身份验证
- ASP.NET生命周期中的验证以及身份验证模块(理解以后可以自定义验证方法)
- 1重写MemberProvider系列文章之用户身份验证
- 应用层之用户-服务器交互: 身份验证与cookie
- 身份验证:From验证
- ASP.NET面向角色授权之:自定义Forms用户验证与授权
- Cas单点登录(5)数据库验证用户之自定义密码加密
- 基本用户身份验证
- 再谈CLR:CLR的版本
- 再谈CLR:目标平台的问题
- 再谈CLR:查看程序集的依赖关系
- 再谈CLR: .NET 4.0新功能:Mscoree.dll + Mscoreei.dll=更少的Reboot (上)
- 用户身份验证之Windows验证
- 用户验证之自定义身份验证
- 自定义TraceListener的例子
- LINQ to SQL的快速入门例子
- 通过反射得到类型的所有成员
- 如何在代码中执行应用程序安全性检查(声明式和代码式)
- 如何查找某个特定证书
- 如何读取计算机上面所有的证书信息
- 再谈CLR:构造之谜
- 再谈CLR:关于类型的拷问