Windows登录功能使用C#实现的示例


本文摘自PHP中文网,作者黄舟,侵删。

这篇文章主要介绍了C#实现的WINDOWS登录功能,结合实例形式分析了简单的Windows图形化登陆功能实现技巧,需要的朋友可以参考下

本文实例讲述了C#实现的WINDOWS登录功能。分享给大家供大家参考,具体如下:


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

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Runtime.InteropServices;

using System.Security.Principal;

namespace yutest

{

  public partial class _Default : System.Web.UI.Page

  {

    [DllImport("advapi32.dll", CharSet = CharSet.Auto)]

    public static extern bool LogonUser(string lpszUsername,string lpszDomain,string lpszPassword,int dwLogonType,int dwLogonProvider,out int phToken);

    protected void Page_Load(object sender, EventArgs e)

    {

      string aaa = System.Threading.Thread.CurrentPrincipal.Identity.Name;

      //string bbb = System.Threading.Thread.CurrentPrincipal.Identity.n;

      //System.Environment.UserDomainName

      //System.Environment.UserName

    }

    protected void Button1_Click(object sender, System.EventArgs e)

    {

      //验证用户的输入是否为空

      if (tDomain.Text.Trim().Length > 0 && tUserName.Text.Trim().Length > 0&& tPassword.Text.Trim().Length > 0)

      //调用函数Login(string UserName, string Password, string Domain)

        //实现Windows登录

        if (Login(tUserName.Text.Trim(), tPassword.Text.Trim(),tDomain.Text.Trim()) == true)

        //显示登录成功信息

          LoginMsg.Text = "登录成功!!!";

          LoginMsg.Visible = true;

          return;

        }

        else

        //显示登录失败信息

          LoginMsg.Text = "登录失败,请重新输入用户名称、密码及其系统域名!!!";

          LoginMsg.Visible = true;

        }

      }

    }

    private bool Login(string UserName, string Password, string Domain)

    {    //获取用户名称和系统域名

      string text1 = Domain.Trim();

      string text2 = UserName.Trim();

      text2 = text2.Replace("/", @"\");   //处理符号“/”

      int num1 = text2.IndexOf('\\');    //获取符号“\”的索引

      if (num1 != -1)

      //格式化用户名称和系统域名

        text1 = text2.Substring(0, num1);

        text2 = text2.Substring(num1 + 1);

      }

      else

      //格式化用户名称和系统域名

        num1 = text2.IndexOf('@');

        if (num1 != -1)

        {

          text1 = text2.Substring(num1 + 1);

          text2 = text2.Substring(0, num1);

        }

      }

      //调用函数AuthenticateUser()实现用户Windows登录

      return AuthenticateUser(text2, Password.Trim(), text1);

    }

    private bool AuthenticateUser(string UserName, string Password,string Domain)

    {       //设置用户登录成功的标志

      bool flag1 = false;

      try

      {

        int num1; IntPtr ptr1;

        //调用Windows登录的API

        if (!LogonUser(UserName, Domain, Password, 2, 0, out num1))

        //返回登录结果

          return flag1;

        }

        //调用.NET中的Windows登录

        ptr1 = new IntPtr(num1);

        WindowsIdentity identity1 = new WindowsIdentity(ptr1);

        WindowsPrincipal principal1 = new WindowsPrincipal(identity1);

        HttpContext.Current.User = principal1;

        //设置系统Cookie和重定向页面

        FormsAuthentication.SetAuthCookie(principal1.Identity.Name, false);

        FormsAuthentication.RedirectFromLoginPage(UserName, false);

        flag1 = true;

      }

      catch (Exception) { }

      return flag1;

    }

  }

}

以上就是Windows登录功能使用C#实现的示例的详细内容!

相关阅读 >>

代码分析:在.net core中使用ref和span<t>提高程序性能

javascript client 如何获取 telerik radgrid的值

asp .net 面试题及答案分享

有关c#工厂模式简单讲解

c#中关于async与await的使用详解

c# 实现截图功能的操作实例

.net实现简易的文件增量备份程序

入门级的.net mvc 实例

详细介绍使用c#实现Windows form调用r进行绘图与显示的方法(图)

c#/.net易错的几点

更多相关阅读请进入《Windows》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...