Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login Page in ASP.NET application with FormsAuthentication access denied

Tags:

c#

asp.net

I've got a webapp running that needs users to login. Webconfig:

    <!--Logging in stuff-->
    <authentication mode="Forms">
        <forms loginUrl="login.aspx" timeout="2880"/>
    </authentication>
    <authorization>
        <deny users="?"/>
    </authorization>

And in the login.aspx page (doubled checked the name) I have the following logic after verifying the user credentials using my own database:

    if (checkCredentials.searchCredentials(attemptedName, passwordBox.Text) != null)
            {
                FormsAuthentication.RedirectFromLoginPage(attemptedName,false);
            }

I know the if statement works, as it did with a previous method I used for logging in.

However, when I run the application, the login page opens up immediately with error 401.2.

like image 595
Laura Duffy Avatar asked Oct 15 '25 23:10

Laura Duffy


1 Answers

Since you are using Visual Studio 2017, the first thing you need to check is if Microsoft.AspNet.FriendlyUrls package is included. Go through following steps.

  • comment the line in Global.asax that says RouteConfig.RegisterRoutes(RouteTable.Routes); and try your page now. But, make sure to clear the cache in your browser else the old cached version of this URL with 401.2 error will keep showing.
  • If you still see some issues, then just remove the above package by selecting Solution node in solution explorer and then going to Tools => NuGet Package Manager => Manage Packages for solution; check in Installed list for this package, select it and select the solution checkboxes on right,then click on uninstall button.

Below are some other things that you need to make sure.

Try changing your forms tag in web config to following. Change the value of defaultUrl and timeout according to your requirements.

<authentication mode="Forms">
<forms loginUrl="login.aspx" defaultUrl="home.aspx" 
   slidingExpiration="true" timeout="20" name=".Auth" protection="All">
</forms>
</authentication>
<authorization>
  <deny users="?" />
  <allow users="*" />
</authorization>

Also, your C# code must be in Login button click event; if it's anywhere else then also you could see issues.

Allow Login.aspx for all unauthenticated users. Add this configuration just before </configuration> at end of web config file. Enter the path for Login.aspx if its not in root like Security/login.aaspx if the page is under Security folder of root.

 <location path="Login.aspx">
    <system.web>
      <authorization>
        <allow users="?" />
        <deny users="*" />
      </authorization>
    </system.web>
 </location>

Open the IIS Management console by going to Control Panel > Administrative Tools > Internet Information Services Manager. Then, expand the websites node and select the website you are using. Now double click Authentication in right pane and make sure Anonymous and Forms authentication are enabled and other options are disabled as shown in following screenshot: Security settings in IIS website

like image 135
Sunil Avatar answered Oct 18 '25 11:10

Sunil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!