⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowsauth.aspx

📁 书籍教程 >> .Net教程 >> asp.net编程精彩百例
💻 ASPX
字号:
<%@ Register TagPrefix="Acme" Namespace="Acme" Assembly="QSTools" %>
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/aspplus/util/SrcRef.ascx"%>

<!-- #include virtual="/quickstart/aspplus/include/header.inc" -->

<h4>基于 Windows 的身份验证</h4>
当使用 ASP.NET Windows 身份验证时,ASP.NET 将 <b>WindowsPrincipal</b> 对象附加到当前请求。该对象由 URL 身份验证使用。应用程序也可以以编程方式用它来确定请求标识是否在给定角色中。

<p><Acme:TabControl runat="server">
<Tab Name="C#">
if(User.IsInRole("Administrators")) {
    DisplayPrivilegedContent();
}
</Tab>

<Tab Name="VB">
If User.IsInRole("Administrators") Then
    DisplayPrivilegedContent()
End If
</Tab>

<Tab Name="JScript">
if(User.IsInRole("Administrators")) {
    DisplayPrivilegedContent();
}
</Tab>

</Acme:TabControl><p>

<b>WindowsPrincipal</b> 类按 NT 组成员关系确定角色。应用程序若要确定自己的角色,可以在它们的 Global.asax 文件中处理 <b>WindowsAuthentication_OnAuthenticate</b> 事件并将自己实现 <b>System.Security.Principal.IPrincipal</b> 的类附加到请求,如下面的示例所示:
<p><Acme:TabControl runat="server">
<Tab Name="C#">
// Create a class that implements IPrincipal
public class MyPrincipal : IPrincipal {
  // implement application-defined role mappings
}

// In a Global.asax file:
public void WindowsAuthentication_OnAuthenticate(Object Source, WindowsAuthenticationEventArgs e) {
  // Attach a new application-defined class that implements IPrincipal to
  // the request.
  // Note that since IIS has already performed authentication, the provided
  // identity is used.
  e.User = new MyPrincipal(e.Identity);
}
</Tab>

<Tab Name="VB">
' Create a class that implements IPrincipal
Public Class MyPrincipal : Inherits IPrincipal
  ' Implement application-defined role mappings
End Class

' In a Global.asax file
Public Sub WindowsAuthentication_OnAuthenticate(Source As Object, e As WindowsAuthenticationEventArgs)
  ' Attach a new application-defined class that implements IPrincipal to
  ' the request.
  ' Note that since IIS has already performed authentication, the provided
  ' identity is used.
  e.User = New MyPrincipal(e.Identity)
End Sub
</Tab>

<Tab Name="JScript">
// Create a class that implements IPrincipal.
public class MyPrincipal implements IPrincipal {
  // Implement application-defined role mappings.
}

// In a Global.asax file
public function WindowsAuthentication_OnAuthenticate(Source:Object, e:WindowsAuthenticationEventArgs) : void {
  // Attach a new application-defined class that implements IPrincipal to
  // the request.
  // Note that since IIS has already performed authentication, the provided
  // identity is used.
  e.User = new MyPrincipal(e.Identity);
}
</Tab>

</Acme:TabControl><p>

<p>
下面的示例显示如何访问已经过验证的用户的名称,该名称可用作 <b>User.Identity.Name</b>。熟悉 ASP 的程序员应注意,该值仍可用作 <i>AUTH_USER</i> 服务器变量:
<p>

<Acme:LangSwitch runat="server">
  <CsTemplate>
<Acme:SourceRef
  RunSample="/quickstart/aspplus/samples/security/WindowsAuth/CS/windowsauth.aspx"
  ViewSource="/quickstart/aspplus/samples/security/WindowsAuth/windowsauth.src"
  Icon="/quickstart/aspplus/images/windowsauth.gif"
  Caption="C# Windows 身份验证"
  runat="server" />
  </CsTemplate>
  <VbTemplate>
<Acme:SourceRef
  RunSample="/quickstart/aspplus/samples/security/WindowsAuth/VB/windowsauth.aspx"
  ViewSource="/quickstart/aspplus/samples/security/WindowsAuth/windowsauth.src"
  Icon="/quickstart/aspplus/images/windowsauth.gif"
  Caption="VB Windows 身份验证"
  runat="server" />
  </VbTemplate>
  <JsTemplate>
<Acme:SourceRef
  RunSample="/quickstart/aspplus/samples/security/WindowsAuth/JS/windowsauth.aspx"
  ViewSource="/quickstart/aspplus/samples/security/WindowsAuth/windowsauth.src"
  Icon="/quickstart/aspplus/images/windowsauth.gif"
  Caption="JScript Windows 身份验证"
  runat="server" />
  </JsTemplate>
</Acme:LangSwitch>

<p>

<!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -