【问题标题】:MVC 5 Bypass Forms Authentication for Windows Authenticated usersMVC 5 绕过 Windows 身份验证用户的表单身份验证
【发布时间】:2016-03-19 12:48:03
【问题描述】:

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

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

【问题讨论】:

    标签: c# asp.net asp.net-mvc-5 windows-authentication form-authentication


    【解决方案1】:

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

     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 !"
        }
    

    【讨论】:

    • 非常感谢@Rahul。你能告诉我我应该把这段代码放在哪里吗?抱歉,我对 asp.net 和 MVC 很陌生。
    • 您应该在Forms Authentication 代码逻辑之前使用此代码。
    • 所以@Rahul 如果我使用“个人用户帐户”模板创建一个简单的 MVC 5 应用程序,那么它将进入 AccountController 登录方法吗?
    • 再次感谢@Rahul。我会试试的。对我来说看起来很复杂 :) 使用 AD 用户名和密码验证的功能也是如此。我在想 MVC 应该自动检测用户是否已经在网络上。一个请求-> 是否可以在 github 上发布一个具有上述功能的简单项目。我相信其他人也会受益。
    • 我没有这方面的示例项目。但是你可以在这里添加你的疑问,我会回答它
    猜你喜欢
    • 2023-03-22
    • 2013-10-30
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    相关资源
    最近更新 更多