返回页首
Windows 身份验证
如果应用程序用户使用可由服务器进行身份验证的 Windows 帐户(例如,在 Intranet 方案中),请使用 Windows 身份验证。
如果将 ASP.NET 配置为使用 Windows 身份验证,则 IIS 使用配置的 IIS 身份验证机制执行用户身份验证。图 4 显示了这种情况。
图 4. ASP.NET Windows 身份验证使用 IIS 验证调用者的身份
已验证调用方(如果将 IIS 配置为使用匿名身份验证,则它可能是匿名 Internet 用户帐户)的访问令牌可供 ASP.NET 应用程序使用。请注意以下方面:
•
这将允许 ASP.NET FileAuthorizationModule 使用原调用者的访问令牌,对请求的 ASP.NET 文件执行访问检查。
重要 ASP.NET 文件授权只对映射到 Aspnet_isapi.dll 的文件类型执行访问检查。
•
文件授权不要求使用模拟。如果启用了模拟,则应用程序执行的所有资源访问均使用被模拟的调用者的标识。在这种情况下,请确保附加到资源上的 ACL 包含一个访问控制项 (ACE),它可授予原调用者标识至少“读”访问权限。
标识已验证身份的用户
ASP.NET 将 WindowsPrincipal 对象与当前的 Web 请求关联起来。这包含已验证身份的 Windows 用户的标识以及该用户所属的角色列表。在 Windows 身份验证中,角色列表是由用户所属的 Windows 组集组成的。
以下代码显示如何获取已验证 Windows 用户的标识,以及如何执行简单的角色授权测试。 WindowsPrincipal user = User as WindowsPrincipal;
if (null != user)
{
string username = user.Identity.Name;
// Perform a role check
if ( user.IsInRole(@"DomainName\Manager") )
{
// User is authorized to perform manager functionality
}
}
else
{
// Throw security exception as we don''''t have a WindowsPrincipal
}
返回页首
窗体身份验证
当使用窗体身份验证时,如果未验证身份的用户试图访问受保护的文件或资源(其中,URL 授权拒绝用户访问),该用户所触发的事件顺序如图 5 所示。
图 5. 窗体身份验证的事件顺序
以下描述图 5 所示的事件顺序:
1.
用户发出一个针对 Default.aspx 的 Web 请求。
由于启用了匿名访问,所以 IIS 允许此类请求。ASP.NET 检查 <authorization> 元素并查找 <denyusers=?" > 元素。
2.
将用户重定向到由 <forms> 元素的 loginUrl 属性指定的登录页 (Login.aspx)。
3.
用户提供凭证并提交登录窗体。
4.
根据数据存储(SQL Server 或 Active Directory)检验凭证,并检索角色(可选)。如果要使用基于角色的授权,则必须检索角色列表。
5.
使用 FormsAuthenticationTicket 创建一个 Cookie 并将其发回客户端。将角色存储到票证中。通过在票证中存储角色列表,可以避免为相同用户的每个后续 Web 请求访问数据库重新检索列表。
6.
客户端重定向将用户重定向到原请求页 (Default.aspx)。
7.
在 Application_AuthenticateRequest 事件处理程序(位于 Global.asax)中,使用票证创建 IPrincipal 对象并将其存储在 HttpContext.User 中。
ASP.NET 检查 <authorization> 元素并查找 <denyusers=?" > 元素。但此时对用户进行身份验证。
ASP.NET 检查 <authorization> 元素以确保用户在 <allow> 元素中。
授予用户访问 Default.aspx 的权限。
窗体身份验证的开发步骤
以下列表重点介绍实现窗体身份验证时必须执行的主要步骤:
1.
将 IIS 配置为使用匿名访问。
2.
将 ASP.NET 配置为使用窗体身份验证。
3.
创建登录 Web 窗体并检验所提供的凭证。
4.
从自定义数据存储中检索角色列表。
5.
创建窗体身份验证票证(在票证中存储角色)。
6.
创建一个 IPrincipal 对象。
7.
将 IPrincipal 对象放到当前 HTTP 上下文中。
8.
基于用户名/角色成员身份对用户授权。
将 IIS 配置为使用匿名访问
必须在 IIS 中将应用程序的虚拟目录配置为使用匿名访问。
将 IIS 配置为使用匿名访问的操作步骤
1.
启动 Internet 信息服务管理工具。
2.
选择应用程序的虚拟目录,右击,然后单击“属性”。
3.
单击“安全目录”。
4.
在“匿名访问”和“身份验证控制”组中,单击“编辑”。
5.
选择“匿名访问”。
将 ASP.NET 配置为使用窗体身份验证
下面显示一个配置示例。 <authentication mode="Forms">
<forms name="MyAppFormsAuth"
loginUrl="login.aspx"
protection="Encryption"
timeout="20"
path="/" >
</forms>
</authentication>
创建登录 Web 窗体并验证提供的凭证
根据 SQL Server 数据库或 Active Directory 检验凭证。
更多信息
•
请参见“How To Use Forms Authentication with SQL Server 2000”。
•
请参见“How To Use Forms Authentication with Active Directory”。
从自定义数据存储中检索角色列表
从 SQL Server 数据库表中获取角色,或从在 Active Directory 内配置的组/分发列表中获取角色。详细信息请参考前面的资源。
创建窗体身份验证票证
在票证中存储检索到的角色。以下代码演示了此过程。 // This event handler executes when the user clicks the Logon button
// having supplied a set of credentials
private void Logon_Click(object sender, System.EventArgs e)
{
// Validate credentials against either a SQL Server database
// or Active Directory
bool isAuthenticated = IsAuthenticated( txtUserName.Text,
txtPassword.Text );
if (isAuthenticated == true )
{
// Retrieve the set of roles for this user from the SQL Server
// database or Active Directory. The roles are returned as a
// string that contains pipe separated role names
// for example "Manager|Employee|Sales|"
// This makes it easy to store them in the authentication ticket
string roles = RetrieveRoles( txtUserName.Text, txtPassword.Text );
// Create the authentication ticket and store the roles in the
// custom UserData property of the authentication ticket
FormsAuthenticationTicket authTicket = new
FormsAuthenticationTicket(
1, // version
txtUserName.Text, // user name
DateTime.Now, // creation
DateTime.Now.AddMinutes(20),// Expiration
false, // Persistent
roles ); // User data
// Encrypt the ticket.
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
// Create a cookie and add the encrypted ticket to the
// cookie as data.
HttpCookie authCookie =
new HttpCookie(FormsAuthentication.FormsCookieName,
encryptedTicket);
// Add the cookie to the outgoing cookies collection.
Response.Cookies.Add(authCookie);
// Redirect the user to the originally requested page
Response.Redirect( FormsAuthentication.GetRedirectUrl(
txtUserName.Text,
false ));
}
}
创建一个 IPrincipal 对象
在位于 Global.asax 的 Application_AuthenticationRequest 事件处理程序中创建 IPrincipal 对象。除非您需要基于角色的扩展功能,否则,请使用 GenericPrincipal 类。在本范例中创建一个实现 IPrincipal 的自定义类。
将 IPrincipal 对象放到当前的 HTTP 上下文中
GenericPrincipal 对象的创建过程如下所示。 protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
// Extract the forms authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if(null == authCookie)
{
// There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch(Exception ex)
{
// Log exception details (omitted for simplicity)
return;
}
if (null == authTicket)
{
// Cookie failed to decrypt.
return;
}
// When the ticket was created, the UserData property was assigned a
// pipe delimited st上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] ... 下一页 >> [C语言系列]NET 中C#的switch语句的语法 [系统软件]托拽Explore中的文件到VB.net的窗口 [系统软件]Boost库在XP+Visual C++.net中的安装 [常用软件]新配色面板:Paint.Net3.0RC1官方下载 [常用软件]用内建的“Net Meeting”聊天 [VB.NET程序]Henry的VB.NET之旅(三)—共享成员 [VB.NET程序]Henry的VB.NET之旅(二)—构造与析构 [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体 [VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET) [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
|