asp.net权限认证:Windows认证

Windows认证简单介绍

Windows认证的操作会比较简单,其主要是把用户的交给IIS认证,而且还是一种比较安全的认证哦。

在一些企业内部的工作流系统中,都会要求使用Windows认证,因为他不论对开发者还是对最终使用用户来说,都比较容易操作。

 

今天我们也来试试,先创建一个demo

 

 

新建Default.aspx页面

先不管他,让他空着,他的cs页面如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
 
namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var authenticationType = System.Web.HttpContext.Current.User.Identity.AuthenticationType;
            var domainUserName = System.Web.HttpContext.Current.User.Identity.Name;
 
            Response.Write("域账号:" + domainUserName + "<br/>");
            Response.Write("认证类型:" + authenticationType + "<br/>");
        }
    }
}

web.config配置文件

1
2
3
4
5
6
<configuration>
  <system.web>
    ...
    <authentication mode="Windows" />
  </system.web>
</configuration>

IIS身份认证中关闭其他认证,只保留“Windows身份认证”  

运行项目

 

 

 项目通过IIS拿到domainUserName,然后剩下的操作获取用户权限等,就跟Forms认证是一样了,当然了,我们还可以获取更加详细的用户信息

修改default.aspx.cs

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var domainUserName = System.Web.HttpContext.Current.User.Identity.Name;
            var authenticationType = System.Web.HttpContext.Current.User.Identity.AuthenticationType;
 
            Response.Write("域账号:" + domainUserName + "<br/>");
            Response.Write("认证类型:" + authenticationType + "<br/>");
 
            var user = this.GetUserInfo(domainUserName);
            if (user != null)
            {
                Response.Write("登录名:" + user.SAMAccountName + "<br/>");
                Response.Write("短名称:" + user.GivenName + "<br/>");
                Response.Write("名称:" + user.CN + "<br/>");
                Response.Write("邮件:" + user.Email + "<br/>");
            }
        }
 
        private UserInfo GetUserInfo(string domainUserName)
        {
            try
            {
                if (string.IsNullOrEmpty(domainUserName))
                {
                    return null;
                }
 
                var userArr = domainUserName.Split('\\');
                var domain = userArr[0];
                var loginName = userArr[1];
 
                var entry = new DirectoryEntry(string.Concat("LDAP://", domain));
                var search = new DirectorySearcher(entry);
                search.Filter = string.Format("(SAMAccountName={0})", loginName);
                search.PropertiesToLoad.Add("SAMAccountName");
                search.PropertiesToLoad.Add("givenName");
                search.PropertiesToLoad.Add("cn");
                search.PropertiesToLoad.Add("mail");
 
                var result = search.FindOne();
                if (result != null)
                {
                    var info = new UserInfo();
                    info.SAMAccountName = result.Properties["SAMAccountName"][0].ToString();
                    info.GivenName = result.Properties["givenName"][0].ToString();
                    info.CN = result.Properties["cn"][0].ToString();
                    info.Email = result.Properties["mail"][0].ToString();
                    return info;
                }
            }
            catch
            { }
 
            return null;
        }
 
        public sealed class UserInfo
        {
            public string SAMAccountName;
            public string GivenName;
            public string CN;
            public string Email;
        }
    }

再次运行项目

 

 完美!至此,所有必要操作都已经做完,是不是很简单?刚开始接触的童鞋可能会问,既然是认证怎么不用输入用户名密码?

 windows认证就是那么任性!不需要用户输入用户口令,直接通过IIS获取当前用户的域账户名称,认证过程在你登录电脑的时候就已经完成了

 

如何切换用户

细心的童鞋可能发现了一个问题:如何切换用户?

 这是一个比较现实的问题,因为不能保证企业内所有人都会有一台电脑办公。

 那怎么办呢?有2种方法

1、是退出当前用户,登录另外的用户

2、调整浏览器默认设置,强制输入用户口令,如下

 

我们再试试运行项目

 

成功了,这次输入口令后刷新浏览器不会再弹出,只有关闭浏览器才会重新要求输入

 

asp.net权限认证系列

  1. asp.net权限认证:Forms认证
  2. asp.net权限认证:HTTP基本认证(http basic)
  3. asp.net权限认证:Windows认证
  4. asp.net权限认证:摘要认证(digest authentication)
  5. asp.net权限认证:OWIN实现OAuth 2.0 之客户端模式(Client Credential)
  6. asp.net权限认证:OWIN实现OAuth 2.0 之密码模式(Resource Owner Password Credential)
  7. asp.net权限认证:OWIN实现OAuth 2.0 之授权码模式(Authorization Code)
  8. asp.net权限认证:OWIN实现OAuth 2.0 之简化模式(Implicit)
posted @   ljr忒修斯之船  阅读(2511)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示