【问题标题】:Validate user in intranet application在 Intranet 应用程序中验证用户
【发布时间】:2017-07-01 12:01:12
【问题描述】:

我需要使用 Windows 凭据来允许我的用户登录 Intranet 应用程序。我打算创建一个包含用户名和角色的表,并将Environment.UserName 的用户名与我的表进行比较。转发这个有多安全?有没有更好的方法来完成这项任务?

提前谢谢你。

【问题讨论】:

    标签: c#


    【解决方案1】:

    您可以使用活动目录身份验证来做到这一点:

    下面是您可以在应用程序中尝试的示例代码。

            string domainUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            string[] paramsLogin = domainUser.Split('\\');
    
            string domain = paramsLogin[0].ToString();
            string LdapPath = "";
            string strDomainPath = DomainPath();
    
            LdapPath = string.Format("LDAP://{0}/{1}", DomainName, strDomainPath);
    
            string username = LoginUser.UserName;
            string password = LoginUser.Password;           
    
    
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password);
            try
            {
                // Bind to the native AdsObject to force authentication.
                Object obj = entry.NativeObject;
                DirectorySearcher search = new DirectorySearcher(entry);
                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();
                if (result != null)
                {
                    IsLoginSucess = true;
                    //Do your stuff here
                }
    
                // Update the new path to the user in the directory
                LdapPath = result.Path;
                string _filterAttribute = (String)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                IsLoginSucess = false;                
            }
    

    【讨论】:

      【解决方案2】:

      如果您在 Windows 应用程序中进行开发,请查看以下内容:

      Authenticate user in WinForms (Nothing to do with ASP.Net) System.Security.Principal.WindowsIdentity.GetCurrent() 将为您提供当前的 Windows 用户

      如果你正在开发一个 Web 应用程序,有内置的支持,你需要在你的 web.config 文件中有相关的条目

      Using windows authentication in asp.net with c#

      HttpContext.Current.User.Identity 将为您获取用户身份

      【讨论】:

        猜你喜欢
        • 2011-11-26
        • 2010-09-09
        • 2019-05-28
        • 1970-01-01
        • 2020-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多