简体   繁体   中英

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

Once possible reason why the application might be failing when deployed on IIS is because the account you configured it to run under doesn't have sufficient privileges to access the Active Directory server. By default the application pool in IIS runs under a built-in ApplicationPoolIdentity account which is a local to the server account. You should go to the settings of the application pool in IIS and select a domain account with sufficient privileges.

This works when running locally in VS because you are running your application with your account which is probably a domain account with access to Active Directory.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM