Tuesday, August 10:53 AM
when i wrote about using simple membership in ASP. NET Web Pages a little while ago, commenter akshayms asked "How can I use Windows authentication"? Simple membership uses a login form and a membership database for managing a site ' s users. In contrast, Windows authentication just uses your existing Windows login credentials; No need to log in separately. Windows Auth is useful for the intranet sites, like on a corporate network.
When the question first came up, I asked around, because I hadn ' t played with it myself. The first answer was "Just as in ' normal ' asp.net!", which are to say, by setting the authentication mode in the Applicat Ion ' s Web. config file to "Windows." (documentation.) Like this:
<authentication mode= "Windows"/>
It turned out, tho, which this didn ' t Entirely work. Anyway, long story short, the It looks like your do this:
- Disable Simple membership.
- Require authentication. (Duh, right?) Hold that thought.)
(Windows authentication also needs to is enabled, but that's the default in ASP, so you don ' t actually need to Explici tly switch on.)
You can do these by creating a Web. config file in the Web Pages application and adding the following to it. (Highlights for the interesting bits.)
<?xml version= "1.0"?>
<configuration>
<appSettings>
<add key= "Enablesimplemembership" value= "false"/>
</appSettings>
<system.web>
<compilation debug= "false" targetframework= "4.0"/>
<authorization>
<deny users= "?" />
</authorization>
</system.web>
</configuration>
The linedeny users="?"Is the bit I mentioned Earlier-this denies access to anonymous users, which are to say that it requires the user to be AU Thenticated. As shown here, this would require authentication to access anything in the site. In a intranet site, that's probably fine, since none of your users are probably anonymous.
Then on a page, you can do this:
@WebSecurity.CurrentUserName
... and/or do all the other membership stuff that's supported for Windows authentication in the base membership system. (not just the features of simple membership.)
However, problem. If you ' re testing your site using IIS Express, which are the default testing server for WebMatrix, you get an "Access Denie D "error. Oh, bother.
The Fix to ThisIssue is to make a change in theapplicationhost.configFile, which is (as a might remember) in the following folder:
C:\users\[you]\documents\iisexpress\config
In the config file, find thewindowsAuthenticationElement and change itsenabledattribute totrue. Like this:
<windowsAuthentication enabled="true">
Restart WebMatrix If you happened to has it open whilst doing all this.
This last fix-theapplicationhost.config-is a machine-wide setting. If you want to configure Windows authentication for IIS Express for only specific Folders/apps, you can use a<location>Tag, which lets you apply the configuration settings to specific files and folders in your site. (Info:location Element, how To:control Authorization Permissions on an ASP.)
The<location>Tag might look like this if you wanted to use Windows authentication in IIS Express for the application namedWinauthTest:
<location path= "Winauthtest" >
<system.webServer>
<security>
<authentication>
<windowsauthentication enabled= "true"/>
</authentication>
</security>
</system.webServer>
</location>
One final note. In Visual Studio, it's easier to configure IIS Express to the use of Windows authentication on a per-project basis. Open the project, and in solution Explorer, select the project (parent) node, then press F4 to view properties. Then just set to WindowsAuthentication true:
Credit:this issue was actually investigated and solved by Erik Porter, who are the program Manager for Web Pages stuff. I just wrote it up. :-)
Using Windows Authentication in ASP. NET Web Pages