asp.net 域账号认证

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获取当前用户的域账户名称,认证过程在你登录电脑的时候就已经完成了

转载来自:https://www.cnblogs.com/lanxiaoke/p/6354502.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值