C#实现的WINDOWS登录功能示例
作者:dodo-yufan 时间:2022-09-04 05:10:03
本文实例讲述了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#程序设计有所帮助。
标签:C#,WINDOWS,登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA回顾:封装,继承,多态
2021-09-26 20:17:59
![](https://img.aspxhome.com/file/2023/7/67627_0s.png)
Mybatis generator如何自动生成代码
2023-08-13 10:15:15
![](https://img.aspxhome.com/file/2023/0/58180_0s.png)
关注Ionic底部导航按钮tabs在android情况下浮在上面的处理
2023-10-01 05:38:17
![](https://img.aspxhome.com/file/2023/9/137189_0s.jpg)
Android Volley框架全面解析
2022-05-11 15:33:15
![](https://img.aspxhome.com/file/2023/8/126668_0s.jpg)
Android中自定义ImageView添加文字说明详解
2022-10-23 18:39:27
![](https://img.aspxhome.com/file/2023/4/126664_0s.png)
SpringBoot自定义启动器Starter流程详解
2022-08-25 08:28:19
![](https://img.aspxhome.com/file/2023/3/119053_0s.png)
Android之来电秀实战示例
2023-10-05 08:02:27
![](https://img.aspxhome.com/file/2023/5/137665_0s.png)
详解java 中Spring jsonp 跨域请求的实例
2023-11-19 02:48:18
![](https://img.aspxhome.com/file/2023/7/59667_0s.png)
不依赖于Activity的Android全局悬浮窗的实现
2022-04-08 00:42:43
Android开发中线程池源码解析
2023-12-19 02:30:57
![](https://img.aspxhome.com/file/2023/5/139395_0s.jpg)
Android程序美化之自定义ListView背景的方法
2023-12-13 17:52:57
Java中的zookeeper常用命令详解
2022-12-07 05:46:35
![](https://img.aspxhome.com/file/2023/2/62752_0s.png)
Android仿微信语音对讲录音功能
2021-10-12 22:13:36
![](https://img.aspxhome.com/file/2023/3/120153_0s.gif)
Android自定义控件实现带数值和动画的圆形进度条
2021-09-09 22:02:46
![](https://img.aspxhome.com/file/2023/1/106031_0s.gif)
Spring@Value使用获取配置信息为null的操作
2021-08-01 23:46:27
详解json string转换为java bean及实例代码
2021-07-04 01:48:26
Spring Security登录添加验证码的实现过程
2021-07-25 23:47:17
![](https://img.aspxhome.com/file/2023/2/119192_0s.png)
解决Android应用冷启动时出现的白屏问题的方法
2023-12-16 02:57:36
手动实现将本地jar添加到Maven仓库
2021-06-12 16:06:49
解决Eclipse/STS中出现Resource is out of sync with the file system的异常问题
2022-02-12 22:35:20