Using Windows authentication in ASP.NET Web Pages

Tuesday, 16 August 2011   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 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 like in ‘normal‘ ASP.NET!", which is to say, by setting the authentication mode in the application‘s Web.config file to "Windows." (Documentation.) Like this:

<authentication mode="Windows" />

It turned out, tho, that this didn‘t entirely work. Anyway, long story short, it looks like you do this:

  • Disable simple membership.
  • Require authentication. (Duh, right? Hold that thought.)

(Windows authentication also needs to be enabled, but that‘s the default in ASP.NET, so you don‘t actually need to explicitly switch that 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 line deny users="?" is the bit I mentioned earlier — this denies access to anonymous users, which is to say that it requires the user to be authenticated. As shown here, this would require authentication to access anything in the site. In an intranet site, that‘s probably fine, since none of your users are probably anonymous.

Then in 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 is the default testing server for WebMatrix, you get an "Access Denied" error. Oh, bother.

The fix to this issue is to make a change in the applicationhost.config file, which is (as you might remember) in the following folder:

C:\Users\[you]\Documents\IISExpress\config

In the config file, find the windowsAuthentication element and change its enabled attribute to true. Like this:

<windowsAuthentication enabled="true">

Restart WebMatrix if you happened to have it open whilst doing all this.

This last fix — the change to applicationhost.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 configuration settings to specific files and folders in your site. (Info: location ElementHOW TO: Control Authorization Permissions in an ASP.NET Application.)

The <location> tag might look like this if you wanted to use Windows authentication in IIS Express for the application named WinauthTest:

<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 use 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 setWindowsAuthentication to true:

Credit: This issue was actually investigated and solved by Erik Porter, who is the Program Manager for Web Pages stuff. I just wrote it up. :-)

时间: 2024-10-10 09:52:04

Using Windows authentication in ASP.NET Web Pages的相关文章

ASP.NET Web Pages (Razor) FAQ

By Tom FitzMacken|February 7, 2014 Print This article lists some frequently asked questions about ASP.NET Web Pages (Razor) and WebMatrix. Software versions used in the tutorial What's the difference between ASP.NET Web Pages, ASP.NET Web Forms, and

ASP.NET Web Pages (Razor) API Quick Reference

By Tom FitzMacken|February 10, 2014 Print This page contains a list with brief examples of the most commonly used objects, properties, and methods for programming ASP.NET Web Pages with Razor syntax. Descriptions marked with "(v2)" were introduc

IIS7下部署asp.net mvc及asp.net web pages的问题

在IIS7下部署asp.net mvc和asp.net web pages一不小心就会遇到文件找不到的错误,如下图所示: 发生这种问题的根本原因在于IIS7考虑了很多兼容性的东西,解决该问题的方法也很简单就是在配置文件中加入如下的配置项:   <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer>   同类型的问题有不少呢:

Customizing Site-Wide Behavior for ASP.NET Web Pages (Razor) Sites

By Tom FitzMacken|February 17, 2014 This article explains how to make site-side settings for pages in an ASP.NET Web Pages (Razor) website. What you'll learn: How to run code that lets you set values (global values or helper settings) for all pages i

ASP.NET Web Pages 的冲突版本问题

随着VS版本和.NET MVC版本.EF的版本的不断更新,虽然很多功能随着版本的提升而更完善,但对于旧版本开发的软件就有点悲催了,或许很多开发者都遇到类似的问题! 最近有一个项目是用.NET MVC3+EXT.NET MVC开发的(当时使用本博客中Ext.Net MVC 配置(2)进行配置的),现在要移植到MVC4的环境中,就遇到了各种各样的问题,都快奔溃了,现在就来总结下: 1.ASP.NET Web Pages版本问题 调试后浏览器提示: 检测到 ASP.NET Web Pages 的冲突版

New for ASP.NET Web Pages: Conditional attributes

from:http://www.mikepope.com/blog/AddComment.aspx?blogid=2353 March 01, 2012 The beta release of ASP.NET Web Pages has been released (for example, as part of the ASP.NET MVC 4 Beta release). There are only a few differences from the December 2011 Dev

五张图概括 什么是 ASP 、 ASP.NET (Web Pages,Web Forms ,MVC )

当你看懂下面这五张图,我相信你对于学习.NET Web开发路线将不陌生!                                               来源: http://www.w3school.com.cn/ ASP   ASP.NET Web Pages Web Forms MVC 建议结合 : http://msdn.microsoft.com/  学习  !

ASP.NET Web Pages ----学习笔记(二)

C#主要Razor语法规则: Razor代码块由@{...}包围 行内表达式以@开始 代码语句以分号结束 变量通过var关键词进行声明 字符串用引用来包围 C#代码对大小写敏感 C#文件的扩展名是.cshtml 内容块 @RenderPage()从不同的文件导入内容 使用布局页面 @RenderBody()内容页必须以Layout指令开头 防止代码泄露 在ASP.NET中,名称以下划线开头的文件无法通过Web来浏览 隐藏敏感信息 在ASP.NET中,隐藏敏感信息常用方法是把这些信息保存在名为"_

如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites]

如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites] 一.布局页面介绍[About Layout Pages] 很多网站有些内容需要显示在各个页面中,比如Header,Footer或者告诉用户已经登录的部分.ASP.NET允许你创建一个单独的文件来包含文本.标签和代码的内容块,从而搭建一个风格整齐的网站.接下来你就可以将这个内容块插入到任何你想要让它展示的页面中.采用这种方法