gpt4 book ai didi

c# - 在 Intranet 应用程序中验证用户

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:40 24 4
gpt4 key购买 nike

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

提前谢谢你。

最佳答案

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

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

        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;
}

关于c# - 在 Intranet 应用程序中验证用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44860678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com