温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用C#怎么实现一个WINDOWS登录功能

发布时间:2021-04-09 18:09:05 来源:亿速云 阅读:202 作者:Leah 栏目:编程语言

这篇文章将为大家详细讲解有关使用C#怎么实现一个WINDOWS登录功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

具体如下:

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;
    }
  }
}

关于使用C#怎么实现一个WINDOWS登录功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI