FormsIdentity 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示使用表單驗證的已驗證使用者識別。 此類別無法獲得繼承。
public ref class FormsIdentity sealed : System::Security::Principal::IIdentity
public ref class FormsIdentity : System::Security::Principal::IIdentity
public ref class FormsIdentity : System::Security::Claims::ClaimsIdentity
[System.Serializable]
public sealed class FormsIdentity : System.Security.Principal.IIdentity
[System.Serializable]
public class FormsIdentity : System.Security.Principal.IIdentity
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(false)]
public class FormsIdentity : System.Security.Claims.ClaimsIdentity
[<System.Serializable>]
type FormsIdentity = class
interface IIdentity
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(false)>]
type FormsIdentity = class
inherit ClaimsIdentity
Public NotInheritable Class FormsIdentity
Implements IIdentity
Public Class FormsIdentity
Implements IIdentity
Public Class FormsIdentity
Inherits ClaimsIdentity
- 繼承
-
FormsIdentity
- 繼承
- 屬性
- 實作
範例
下列程式代碼範例會產生 FormsAuthenticationTicket ,然後根據使用者是否在窗體上提供正確的使用者名稱和密碼,使用它來建立 類別的實例 FormsIdentity 。
<%@ Page Language="C#" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">
protected void Login_Click(object sender, EventArgs e)
{
bool isAuthenticated = false;
if (string.Compare(UserNameTextBox.Text, "UserName", true,
CultureInfo.InvariantCulture) == 0)
{
if (string.Compare(PasswordTextBox.Text, "Password", true,
CultureInfo.InvariantCulture) == 0)
{
isAuthenticated = true;
}
}
else isAuthenticated = false;
// Create the formsIdentity for the user.
CreateformsIdentity(UserNameTextBox.Text, isAuthenticated);
}
private void CreateformsIdentity(string userName, bool isAuthenticated)
{
FormsIdentity formsID;
FormsAuthenticationTicket authenticationTicket;
if (isAuthenticated)
{
// If authentication passed, create a ticket
// as a Manager that expires in 15 minutes.
authenticationTicket = new FormsAuthenticationTicket(1, userName,
DateTime.Now, DateTime.Now.AddMinutes(15), false, "Manager");
}
else
{
// If authentication failed, create a ticket
// as a guest that expired 5 minutes ago.
authenticationTicket = new FormsAuthenticationTicket(1, userName,
DateTime.Now, DateTime.Now.Subtract(new TimeSpan(0, 5, 0)),
false, "Guest");
}
// Create form identity from FormsAuthenticationTicket.
formsID = new FormsIdentity(authenticationTicket);
Response.Clear();
Response.Write("Authentication Type: " + formsID.AuthenticationType +
"<BR>");
// Get FormsAuthenticationTicket from the FormIdentity
FormsAuthenticationTicket ticket = formsID.Ticket;
if (ticket.Expired)
{
Response.Write("Authentication failed, so the role is set to " +
ticket.UserData);
}
else
{
Response.Write("Authentication succeeded, so the role is set to " +
ticket.UserData);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>WebForm1</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="UserIdLabel" runat="server"
style="left: 144px; position: absolute; top: 160px">
User-ID:</asp:Label>
<asp:Label id="PasswordLabel" runat="server"
style="left: 144px; position: absolute; top: 200px">
Password:</asp:Label>
<asp:TextBox id="UserNameTextBox" runat="server"
style="left: 232px; position: absolute; top: 160px;
width:182px; height:22px"></asp:TextBox>
<asp:TextBox id="PasswordTextBox" runat="server"
style="left: 232px; position: absolute; top: 200px;
width:181px; height:22px" TextMode="Password">
</asp:TextBox>
<asp:Button id="Login" runat="server" Text="Login"
style="left: 232px; position: absolute; top: 232px;
width:100px" OnClick="Login_Click"></asp:Button>
</form>
</body>
</html>
<%@ Page Language="vb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
Private Sub Login_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Login.Click
' For the example, the user name must be "UserName" and the password
' must be "Password" for authentication to succeed.
Dim isAuthenticated As Boolean
If String.Compare(UserNameTextBox.Text, "UserName", True, _
CultureInfo.InvariantCulture) = 0 Then
If String.Compare(PasswordTextBox.Text, "Password", True, _
CultureInfo.InvariantCulture) = 0 Then
isAuthenticated = True
End If
Else
isAuthenticated = False
End If
' Create the FormsIdentity for the user.
CreateFormsIdentity(UserNameTextBox.Text, isAuthenticated)
End Sub
Private Sub CreateFormsIdentity(ByVal userName As String, _
ByVal isAuthenticated As Boolean)
Dim formsId As System.Web.Security.FormsIdentity
Dim authenticationTicket As _
System.Web.Security.FormsAuthenticationTicket
If isAuthenticated Then
' If authentication passed, create a ticket
' as a Manager that expires in 15 minutes.
authenticationTicket = _
New FormsAuthenticationTicket(1, userName, DateTime.Now, _
DateTime.Now.AddMinutes(15), False, "Manager")
Else
' If authentication failed, create a ticket
' as a guest that expired 5 minutes ago.
authenticationTicket = _
New FormsAuthenticationTicket(1, userName, DateTime.Now, _
DateTime.Now.Subtract(New TimeSpan(0, 5, 0)), False, "Guest")
End If
' Create form identity from FormsAuthenticationTicket.
formsId = New FormsIdentity(authenticationTicket)
Response.Clear()
Response.Write("Authenticate Type: " & _
formsId.AuthenticationType & "<BR>")
' Get FormsAuthenticationTicket from the FormIdentity
Dim ticket As FormsAuthenticationTicket = formsId.Ticket()
If ticket.Expired Then
Response.Write("Authentication failed, so the role is set to " & _
ticket.UserData)
Else
Response.Write("Authentication succeeded, so the role is set to " & _
ticket.UserData)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>WebForm1</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:Label id="UserIdLabel" runat="server"
style="left: 144px; position: absolute; top: 160px">
User-ID:</asp:Label>
<asp:Label id="PasswordLabel" runat="server"
style="left: 144px; position: absolute; top: 200px">
Password:</asp:Label>
<asp:TextBox id="UserNameTextBox" runat="server"
style="left: 232px; position: absolute; top: 160px;
width:182px; height:22px"></asp:TextBox>
<asp:TextBox id="PasswordTextBox" runat="server"
style="left: 232px; position: absolute; top: 200px;
width:181px; height:22px" TextMode="Password">
</asp:TextBox>
<asp:Button id="Login" runat="server" Text="Login"
style="left: 232px; position: absolute; top: 232px;
width:100px"></asp:Button>
</form>
</body>
</html>
備註
當使用者使用窗體驗證進行驗證時,就會使用 FormsAuthenticationModule 類別FormsIdentity。 使用從窗體驗證 Cookie 或 URL 解密的 ,建立 類別FormsAuthenticationTicket的FormsIdentity實例。 類別的新實例 FormsIdentity 是用來建構新的 GenericPrincipal 物件,該物件設定為 User 目前 HttpContext的屬性值。
建構函式
FormsIdentity(FormsAuthenticationTicket) |
初始化 FormsIdentity 類別的新執行個體。 |
FormsIdentity(FormsIdentity) |
根據指定的識別初始化 FormsIdentity 類別的新執行個體。 |
欄位
DefaultIssuer |
預設核發者:「本機授權單位」。 (繼承來源 ClaimsIdentity) |
DefaultNameClaimType |
預設名稱宣告型別;Name. (繼承來源 ClaimsIdentity) |
DefaultRoleClaimType |
預設角色宣告型別;Role. (繼承來源 ClaimsIdentity) |
屬性
Actor |
取得或設定已授與委派權限之呼叫方的識別。 (繼承來源 ClaimsIdentity) |
AuthenticationType |
取得已驗證識別的類型。 |
BootstrapContext |
取得或設定用來建立此宣告識別的權杖。 (繼承來源 ClaimsIdentity) |
Claims |
取得與這個實體相關聯的宣告集合。 |
CustomSerializationData |
包含衍生類型所提供的任何其他資料。 通常會在呼叫 WriteTo(BinaryWriter, Byte[]) 時設定。 (繼承來源 ClaimsIdentity) |
IsAuthenticated |
取得值,指出是否已進行驗證。 |
Label |
取得或設定此宣告識別的標籤。 (繼承來源 ClaimsIdentity) |
Name |
取得表單識別的使用者名稱。 |
NameClaimType |
取得宣告型別,用來判斷哪些宣告為這個宣告識別的 Name 屬性提供值。 (繼承來源 ClaimsIdentity) |
RoleClaimType |
在此宣告身分識別的宣告中,取得將解譯為 .NET 角色的宣告型別。 (繼承來源 ClaimsIdentity) |
Ticket |
取得表單驗證使用者識別的 FormsAuthenticationTicket。 |