FormsIdentity 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个使用 Forms 身份验证进行了身份验证的用户标识。 此类不能被继承。
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>
注解
使用 FormsIdentity 表单身份验证对用户进行身份验证时, FormsAuthenticationModule 类由 使用。 类的 FormsIdentity 实例是使用 FormsAuthenticationTicket 从 forms-authentication Cookie 或 URL 解密的 创建。 类的新实例FormsIdentity用于构造一个新GenericPrincipal对象,该对象设置为当前 HttpContext的 User 属性的值。
构造函数
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 |
获取 Forms 标识的用户名。 |
NameClaimType |
获取用于确定为此声明标识的 Name 属性提供值的声明的声明类型。 (继承自 ClaimsIdentity) |
RoleClaimType |
获取将解释为此声明标识中声明的 .NET 角色的声明类型。 (继承自 ClaimsIdentity) |
Ticket |
获取 Forms 身份验证用户标识的 FormsAuthenticationTicket。 |