gpt4 book ai didi

c# - MVC 5 绕过 Windows 身份验证用户的表单例份验证

转载 作者:太空狗 更新时间:2023-10-29 20:38:50 24 4
gpt4 key购买 nike

我已经有一个使用 MVC 5 编写的网站,它使用 SQL Server 进行表单例份验证。

现在我可以为已经在办公网络上的用户绕过 Forms Authentication 吗?我还想跟踪用户并应用类似于 Forms Authentication 的规则。谢谢。

最佳答案

是的,你可以做到。这是检查域中用户的代码。首先获取域名并尝试使用域验证用户。如果失败,则继续进行表单例份验证。

 public static string DomainControllerName { get; private set; }
public static string ComputerName { get; private set; }
public static string DomainName { get; private set; }
public static string DomainPath
{
get
{
bool bFirst = true;
StringBuilder sbReturn = new StringBuilder(200);
string[] strlstDc = DomainName.Split('.');
foreach (string strDc in strlstDc)
{
if (bFirst)
{
sbReturn.Append("DC=");
bFirst = false;
}
else
sbReturn.Append(",DC=");

sbReturn.Append(strDc);
}
return sbReturn.ToString();
}
}
public static string RootPath
{
get
{
return string.Format("LDAP://{0}/{1}", DomainName, DomainPath);
}
}
Domain domain = null;
DomainController domainController = null;
try
{
domain = Domain.GetCurrentDomain();
DomainName = domain.Name;
domainController = domain.PdcRoleOwner;
DomainControllerName = domainController.Name.Split('.')[0];
ComputerName = Environment.MachineName;
}
finally
{
if (domain != null)
domain.Dispose();
if (domainController != null)
domainController.Dispose();
}


try
{
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
DirectoryEntry root = new DirectoryEntry(RootPath, txtUserName.Text.Trim(), txtPassword.Text);
DirectorySearcher search = new DirectorySearcher(root);

search.SearchScope = SearchScope.Subtree;
search.Filter = "(sAMAccountName=" + txtUserName.Text.Trim() + ")";
SearchResultCollection results = search.FindAll();

UserPrincipal userP = UserPrincipal.FindByIdentity(ctx, txtUserName.Text.Trim());

if (userP != null && results != null)
{
//Get the user's groups
var groups = userP.GetAuthorizationGroups();
if (groups.Count(x => x.Name == ConfigurationManager.AppSettings["UserGroup"].ToString()) > 0)
{
//Successful login code here
}
else
{
//"Access Denied !";
}
}
else
{
//"User Name or Password is incorrect. Try again !"
}
}
}
catch
{
//"User Name or Password is incorrect. Try again !"
}

关于c# - MVC 5 绕过 Windows 身份验证用户的表单例份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34260250/

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