Search code examples
c#asp.netactive-directorywindows-authentication

Asp.net WebPage with Windows Authentication aganst Active Directory


I have been messing with this all night and it is really frustrating. I got the webpage to display finally but for some reason it is not working meaning that it shoots me back to my error page and states "Could Not Authenticate User Please Try Again!". I cant figure this out as this works perfectly find on my local machine that visual studios is installed on.

Please if someone can take the time to help out with this, ive been fighting with it all day. THanks

Class.cs

public bool IsUserInSetup(string groupName, UserPrincipal user)
    {
        PrincipalContext context = new PrincipalContext(ContextType.Domain, "DomainName");
       GroupPrincipal group = GroupPrincipal.FindByIdentity(context, "GroupName");

        if (user.IsMemberOf(group))
        {
            return true;
        }
        return false;
    }

Authentication.aspx.cs

 if (!IsPostBack)
            {
                if (Request.IsAuthenticated)
                {

                    if (gv.IsUserInSetup("GroupName", user))
                    {
                        Session["userValue"] = UserPrincipal.Current;

                        Response.Redirect("Default.aspx");

                    }
 else
                {
                    lblInfo.Text = "Could Not Authenticate User Please Try Again!";
                    lblInfo.Text += "<br><b>Name: </b>" + User.Identity.Name;
                    lblInfo.Text += "<br><b>Authenticated With: </b>";
                    lblInfo.Text += User.Identity.AuthenticationType;
                }

Solution

  • Got the answer on another forum but I was doing a lot of extra work for no reason. Asp.net has a USER.ISINROLE("GROUPNAME") and i used that.