栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#实现的WINDOWS登录功能示例

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#实现的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;
    }
  }
}

更多关于C#相关内容感兴趣的读者可查看本站专题:《C#常见控件用法教程》、《WinForm控件用法总结》、《C#程序设计之线程使用技巧总结》、《C#数据结构与算法教程》及《C#面向对象程序设计入门教程》

希望本文所述对大家C#程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123303.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号