Example of Windows login function implemented using C#
这篇文章主要介绍了C#实现的WINDOWS登录功能,结合实例形式分析了简单的Windows图形化登陆功能实现技巧,需要的朋友可以参考下
本文实例讲述了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; } } }
The above is the detailed content of Example of Windows login function implemented using C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

For some stubborn software, residual dll files will remain after uninstallation, and this dll file cannot be deleted. Some of them will be bound to the process (generally easy to bind to the expore.exe process) and start up when the computer is turned on. , when deleting a file, you will be prompted that a program is occupied (or a service is running, etc.). Method 1: 1. After pressing the win+r key, an interface will appear, and then enter regedit. The screenshot is as follows: 2. Then press Enter and we You will enter the Registry Editor, the screenshot is as follows: 3. At this time we will find the "HKEY_LOCAL_MACHINE" file in "My Computer", and then click to expand--SOFTWAR

ETA Prime recently showcased a paid software called Lossless Scaling on ROG Ally X. While it doesn't actually improve the actual gaming performance, the software enhances the experience by adding frame generation and resolution scaling. These two can

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

The way to update ByBit exchanges varies by platform and device: Mobile: Check for updates and install in the app store. Desktop Client: Check for updates in the Help menu and install automatically. Web page: You need to manually access the official website for updates. Failure to update the exchange can lead to security vulnerabilities, functional limitations, compatibility issues and reduced transaction execution efficiency.

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Despite the hype surrounding the Qualcomm Snapdragon X Elite, it has been a rather mediocre launch. In our review, we found that the most impressive part of the new Qualcomm Snapdragon X Elite X1E-78-100-powered Asus Vivobook S 15 was the seamlessnes
