FormsAuthenticationModule 类

定义

启用 Forms 身份验证的情况下设置 ASP.NET 应用程序用户的标识。 此类不能被继承。

public ref class FormsAuthenticationModule sealed : System::Web::IHttpModule
public sealed class FormsAuthenticationModule : System.Web.IHttpModule
type FormsAuthenticationModule = class
    interface IHttpModule
Public NotInheritable Class FormsAuthenticationModule
Implements IHttpModule
继承
FormsAuthenticationModule
实现

示例

以下示例使用 FormsAuthentication_OnAuthenticate 事件将当前 HttpContext 的 属性设置为User自定义IPrincipal对象。

public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
{
  if (FormsAuthentication.CookiesSupported)
  {
    if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
    {
      try
      {
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
          Request.Cookies[FormsAuthentication.FormsCookieName].Value);
        
        args.User = new System.Security.Principal.GenericPrincipal(
          new Samples.AspNet.Security.MyFormsIdentity(ticket),
          new string[0]);
      }
      catch (Exception e)
      {
        // Decrypt method failed.
      }
    }
  }
  else
  {
    throw new HttpException("Cookieless Forms Authentication is not " +
                            "supported for this application.");
  }
}
Public Sub FormsAuthentication_OnAuthenticate(sender As Object, _
                                              args As FormsAuthenticationEventArgs)
  If FormsAuthentication.CookiesSupported Then
    If Not Request.Cookies(FormsAuthentication.FormsCookieName) Is Nothing Then
      Try
        Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt( _
          Request.Cookies(FormsAuthentication.FormsCookieName).Value)
        
        args.User = New System.Security.Principal.GenericPrincipal( _
          New Samples.AspNet.Security.MyFormsIdentity(ticket), _
          New String(0) {})
      Catch e As HttpException
        ' Decrypt method failed.
      End Try
    End If
  Else
      Throw New Exception("Cookieless Forms Authentication is not " & _
                            "supported for this application.")
  End If
End Sub

注解

在应用程序的配置文件的 authentication Element (ASP.NET Settings Schema) 元素中将身份验证Mode设置为 时,将 FormsAuthenticationModule current HttpContext.User 属性IPrincipal设置为 Forms 表示当前请求的用户标识的对象。

FormsAuthenticationModule公开一个 Authenticate 事件,该事件使你能够为当前 HttpContext的 属性提供自定义IPrincipal对象User。 通过在 Authenticate ASP.NET 应用程序的 Global.asax 文件中指定名为 FormsAuthentication_OnAuthenticate 的子例程来访问事件。

注意

在 IIS 7.0 中,不支持将 与 一起使用WindowsAuthenticationModuleFormsAuthenticationModule。 有关集成模式下兼容性问题的详细信息,请参阅 将 ASP.NET 应用程序从 IIS 6.0 移动到 IIS 7.0

构造函数

FormsAuthenticationModule()

初始化 FormsAuthenticationModule 类的新实例。

方法

Dispose()

释放 FormsAuthenticationModule 使用的所有资源,内存除外。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
Init(HttpApplication)

初始化 FormsAuthenticationModule 对象。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

事件

Authenticate

应用程序对当前请求进行身份验证时发生。

适用于

另请参阅