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();

  • 相关阅读:
    Docker
    Docker
    Linux
    VUE- 前端插件
    小程序中实现 input 搜索框功能
    Vue 中用delete方式进行axios请求接口,请求状态码报415(Unsupported Media Type)
    关于小程序使用map组件,标记markers时报错误(ret is not defined)
    关于element 框架中table表格选中并切换下一页之前选中数据消失的问题
    vue切换路由时报错 uncaught(in promise) Navigation Duplicated 问题
    2019-09-09 JS面试题(持续更新中)
  • 原文地址:https://www.cnblogs.com/chengeng/p/5660088.html
Copyright © 2011-2022 走看看