Asp.net 仅为域组登录到网站

Asp.net 仅为域组登录到网站,asp.net,c#-4.0,active-directory,Asp.net,C# 4.0,Active Directory,大约一周前我就有这个问题了。我设法让所有Active directory用户登录到该网站,但现在我想将其限制为一个特定的组,比如说“Group1”。实际上,我试图在web.config中使用以下内容,但它总是要求提供凭据,即使我提供了有效的用户,它也不接受 <authentication mode="Windows" /> <authorization> <allow users="DomainName\Group1" /> <

大约一周前我就有这个问题了。我设法让所有Active directory用户登录到该网站,但现在我想将其限制为一个特定的组,比如说“Group1”。实际上,我试图在web.config中使用以下内容,但它总是要求提供凭据,即使我提供了有效的用户,它也不接受

   <authentication mode="Windows" />
  <authorization>
    <allow users="DomainName\Group1" />
    <deny users="*" />
  </authorization>
</system.web>

希望能帮我解决这个问题

你的web.config在我看来是正确的;可能还有其他问题。另外,关于你的三个步骤,你可能会朝着扭动的方向走;我尝试了大多数可能的解决方案,但并没有局限于给定的组名。它只允许所有用户登录,以防我在允许标记中从组更改为一个用户。我还使用user.IsInRole(“域\组1”);但在验证用户登录页面之前,该页面不接受用户名和密码。我认为您的web.config看起来正确;可能还有其他问题。另外,关于您的3个步骤,您可能会朝着正确的方向走;尝试坚持您的实际web.config。我尝试了大多数可能的解决方案,但没有限制到给定的组名。它只允许所有用户登录,以防我在允许标记中从组更改为仅一个用户。我还使用user.IsInRole(“域\组1”);但在验证用户登录页面之前,它不接受用户名和密码
   string dominName = string.Empty;
        string adPath = string.Empty;
        string userName = TextBox1.Text.Trim().ToUpper();
        string strError = string.Empty;

        try
        {
            foreach (string key in ConfigurationSettings.AppSettings.Keys)
            {
                dominName = key.Contains("DirectoryDomain") ? ConfigurationSettings.AppSettings[key] : dominName;
                adPath = key.Contains("DirectoryPath") ? ConfigurationSettings.AppSettings[key] : adPath;

                if (!String.IsNullOrEmpty(dominName) && !String.IsNullOrEmpty(adPath))
                {
                        if (true == AuthenticateUser(dominName, userName, TextBox2.Text, adPath, out strError))
                        {

                            Response.Redirect("default.aspx");// Authenticated user redirects to default.aspx    
                         }
                    dominName = string.Empty;
                    adPath = string.Empty;

                    if (String.IsNullOrEmpty(strError)) break;
                }
            }

            if (!string.IsNullOrEmpty(strError))
            {
                Label3.Visible = true;
                Label3.Text = "Wrong username or password";
            }
        }
        catch
        {

        }

        finally
        {

        }
    }

    public bool AuthenticateUser(string domain, string username, string password, string LdapPath, out string Errmsg)
    {

        Errmsg = "";

        string domainAndUsername = domain + @"\"  + username;

        DirectoryEntry entry = new DirectoryEntry(LdapPath, domainAndUsername, password);

        try
        {
            Object obj = entry.NativeObject;
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "(SAMAccountName=" + username + ")";
            search.Filter = "(Group1=" + username + ")";
            search.PropertiesToLoad.Add("memberOf");
            search.PropertiesToLoad.Add("cn");

            SearchResult result = search.FindOne();

                if (null == result)
                {
                    return false;
                }

                // Update the new path to the user in the directory

                LdapPath = result.Path;
                string _filterAttribute = (String)result.Properties["cn"][0];
            }

           catch (Exception ex)
          {
            Errmsg = ex.Message;
            return false;
            throw new Exception("Error authenticating user." + ex.Message);
        }

        return true;
    }