ASPNetFAQ.com: What is ASP.NET?

Technology posts on ASP.NET, IIS, Windows (+ a little Linux), Cloud Servers, Hosting, and more!
  • Blog Home
Search the site...

Multiple Forms Auth Security Settings In ASP.Net

Tweet
Share
0 Shares

In a previous article I provided some samples to allow password protection of a folder in ASP.Net based on some settings in the root config.web file. Since that article I have had a few people ask if it was possible to secure multiple locations – each potentially with their own security requirements.

Well, the answer is yes, it is possible and it isn’t even very hard. Below is a sample config.web file, that when placed in the web root will secure two different folders. One is /admin/ and the other is /protected/.

Access to the /admin/ folder is controlled in lines 13 through 19. One line 16 it is specified that the only people that can access this folder are people that have authenticated via ASP.Net. It does not matter who the person is, as long as they have provided a valid username and password (noted on lines 06 through 07).

Access to the /protected/ folder is more secure. The setting on line 24 specify that the user “User1” is allowed access to this folder. This line alone is not good enough to trigger the security. It also needs to be specified to deny all users (other than “User1”), which is done by the code on line 25.

As you have probably noted by now, the authorization section will accept either a “deny” or an “allow” statement, so you can specifically control the type of access (or lack of access). You might have also noted that you can use various items for the “users” property. Using “*” means to deny (or allow) everyone; using “?” means to deny (or allow) any known users (users who have not yet authenticated); you can also specify an individual username for this property if you want to limit access to only certain users.

<configuration>
<system.web>
    <authentication mode="Forms">
        <forms name="TestAuthCookie" loginUrl="login.aspx" timeout="30">
            <credentials passwordFormat="Clear">
                <user name="user1" password="pass1"/>
                <user name="user2" password="pass2"/>
            </credentials>
        </forms>
    </authentication>
</system.web>
<location path="admin">
    <system.web>
        <authorization>
            <deny users="?" />
        </authorization>
    </system.web>
</location>
<location path="protected">
    <system.web>2
        <authorization>
            <allow users="user1" />
            <deny users="*" /> 
        </authorization>
    </system.web> 
</location>
</configuration>

As you can see, the config.web file allows for some fairly complex security restrictions once you understand the required format. Wrapping all of these security configurations into the config.web file – as opposed to implementing them with IIS settings – allows a few benefits. The most obvious are: The developer can configure the security themselves without getting a server administrator involved; and deploying the application to multiple servers is easier since all of the settings are actually in the code and no system changes are needed.

More from my site

  • The Best Way to Learn ASP.NET?The Best Way to Learn ASP.NET?
  • Cloud Server FlexibilityCloud Server Flexibility
  • Handler "PHP5x_via_FastCGI" has a bad module "FastCgiModule" in its module listHandler "PHP5x_via_FastCGI" has a bad module "FastCgiModule" in its module list
  • Troubleshooting SSL in IISTroubleshooting SSL in IIS
  • Orchard: Custom Content in Sub-FoldersOrchard: Custom Content in Sub-Folders
Tweet
Share
0 Shares

5 comments on “Multiple Forms Auth Security Settings In ASP.Net”

  1. Chuck says:
    October 2, 2012 at 4:24 pm

    Thanks for taking the time to set this up.
    I do have one problem tho….Oh, I don’t have a problem. I took your two location commands, modified them to point to aspx pages, one to redirect to the login page and another to allow all users. Now its working just fine. Thanks for the help. Saved me many hours of work.
    Chuck

  2. Anh says:
    December 28, 2012 at 2:12 pm

    Thank you,
    I have one question. Can we restrict one website instead of a folder.

    • Anh says:
      December 28, 2012 at 2:14 pm

      Sorry, i mean one page (e.g: secret.aspx).

      • Brad Kingsley says:
        December 29, 2012 at 9:41 am

        Oh, yes, in that case just put the full path and page name into the location field.

    • Brad Kingsley says:
      December 29, 2012 at 9:40 am

      Yes, if you want to protect the entire site rather than a folder, review this article: http://bradkingsley.com/securing-asp-net-pages-forms-authentication-c-and-net-4/

Proverbs 19:20

"Get all the advice and instruction you can, so you will be wise the rest of your life."

A Note On WordPress Hosting

Our main focus is of course .NET, but with a mix of Linux, virtualization, and other technologies. But if you're really looking for the best WordPress hosting specifically, read my WordPress host review to save yourself hassle AND money!




Recent Posts

  • What makes good web hosting?
  • jQuery Mobile C# ASP.NET and N5 Networks Software Repository
  • Open Source Bug Tracking Software and the Orchard Project
  • ASP.NET Development with Dreamweaver MX: Visual QuickPro Guide
  • Kendo UI Sample, ASP.NET Ajax Tutorial & More

Tags

ASP.NET Automation centos CMS css cytanium Development/Coding Email gmail Hosting htaccess http https IIS javascript Learning Linux logparser MySQL nginx openssl OrcsWeb performance PowerShell redirect RHEL security server SherWeb smtp SQL/Databases ssl System Administration telnet terminal tip Troubleshooting Ubuntu virtualization Visual Studio web farm web hosting Windows windows server Wordpress

Categories

  • ASP.net development
  • Development/Coding
  • Hosting
  • IIS (Internet Information Services)
  • SQL/Databases
  • System Administration
  • Virtualization
(c) ASPNETFAQ.com