zoukankan      html  css  js  c++  java
  • AD域账号验证

    public partial class _Default : Page
    {
    [DllImport("advapi32.dll")]
    private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
    protected void Page_Load(object sender, EventArgs e)
    {
    lblLocaAdAccount.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
    string strDomain = txtDomain.Text.Trim();
    string strUserName = txtUserName.Text.Trim();
    string strPassWord = txtPassWord.Text.Trim();

    lblMsg.Text = "验证中…";
    bool isAccount = ValidateUserAccount(strDomain, strUserName, strPassWord);
    if (isAccount)
    {
    lblMsg.Text = "合法用户";
    }
    else {
    lblMsg.Text = "非法用户";
    }


    if (isAccount)
    {
    //完全采用域用户来管理
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
    1, // version
    strUserName, // user name
    DateTime.Now, // issue time
    DateTime.Now.AddHours(1), // expires every hour
    false, // don't persist cookie
    "" // roles
    );
    FormsAuthentication.SetAuthCookie(strUserName, false);
    }
    }

    /// <summary>
    /// 域验证
    /// </summary>
    /// <param name="AstrDomainName">域名称</param>
    /// <param name="AstrDomainAccount">域账号</param>
    /// <param name="AstrDomainPassword">域密码</param>
    /// <returns></returns>
    public bool ValidateUserAccount(string AstrDomainName, string AstrDomainAccount, string AstrDomainPassword)
    {
    const int LOGON32_LOGON_INTERACTIVE = 2; //通过网络验证账户合法性
    const int LOGON32_PROVIDER_DEFAULT = 0; //使用默认的Windows 2000/NT NTLM验证方
    IntPtr tokenHandle = new IntPtr(0);
    tokenHandle = IntPtr.Zero;

    string domainName = AstrDomainName; //域 如:officedomain
    string domainAccount = AstrDomainAccount; //域帐号 如:administrator
    string domainPassword = AstrDomainPassword;//密码
    bool checkok = LogonUser(domainAccount, domainName, domainPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

    return checkok;
    }
    }

    =======================================================================

    其他参考代码 ===============================================================

    =======================================================================

    WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
    WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity);

    bool isDomainUser = currentPrincipal.IsInRole("Domain Users");
    isDomainUser=true; //为域用户

    string userName=System.Environment.UserName;  //获取计算机登录名称

    string computerName=System.Environment.MachineName;//获取计算机名称

    string computerName=Dns.GetHostName();

  • 相关阅读:
    13.App爬取相关库的安装(Charles,Mitmproxy,Appium)
    26.pymysql、pymongo、redis-py安装
    25.安装配置phantomjs
    2.博客随笔加密!!!
    17.scrapy-splash安装-2
    17.docker及scrapy-splash安装-1
    16.Mongodb安装
    scrapy--BeautifulSoup
    scrapy--selenium
    python--随笔一
  • 原文地址:https://www.cnblogs.com/chengeng/p/5660088.html
Copyright © 2011-2022 走看看