DefaultAuthenticationEventArgs(HttpContext) 构造函数

定义

初始化 DefaultAuthenticationEventArgs 类的新实例。

public:
 DefaultAuthenticationEventArgs(System::Web::HttpContext ^ context);
public DefaultAuthenticationEventArgs (System.Web.HttpContext context);
new System.Web.Security.DefaultAuthenticationEventArgs : System.Web.HttpContext -> System.Web.Security.DefaultAuthenticationEventArgs
Public Sub New (context As HttpContext)

参数

context
HttpContext

事件的上下文。

示例

下面的代码示例使用 DefaultAuthentication_OnAuthenticate 事件来测试当前 HttpContext 的 属性是否Usernull。 如果 属性为 null,则示例将当前 HttpContext 的 属性设置为 GenericPrincipalUser 对象,Identity其中 对象的 属性GenericPrincipalGenericIdentityName属性值为“default”的对象。

注意

DefaultAuthentication_OnAuthenticate 事件在事件之前AuthorizeRequest引发。 因此,如果将当前 HttpContext 属性设置为User自定义标识,可能会影响应用程序的行为。 例如,如果使用 FormsAuthentication 类,并且确保只有经过身份验证的用户有权访问您的网站,则通过使用 authorization 节并指定 <deny users="?" />,将导致 deny 忽略此示例中的 元素,因为用户将具有一个名称,该名称为“default”。 相反,应指定 <deny users="default" /> 以确保只有经过身份验证的用户才能访问您的网站。

public void DefaultAuthentication_OnAuthenticate(object sender,
                                                 DefaultAuthenticationEventArgs args)
{
  if (args.Context.User == null)
    args.Context.User = 
      new System.Security.Principal.GenericPrincipal(
        new System.Security.Principal.GenericIdentity("default"),
        new String[0]);
}
Public Sub DefaultAuthentication_OnAuthenticate(sender As Object, _
                                                args As DefaultAuthenticationEventArgs)
  If args.Context.User Is Nothing Then
    args.Context.User = _
      new System.Security.Principal.GenericPrincipal( _
        new System.Security.Principal.GenericIdentity("default"), _
        new String(0) {})
  End If
End Sub

注解

对象DefaultAuthenticationModule使用当前 HttpContext 构造DefaultAuthenticationEventArgs对象,并将其传递给 DefaultAuthentication_OnAuthenticate 事件。

可以使用Context提供给 DefaultAuthentication_OnAuthenticate 事件的 对象的 属性DefaultAuthenticationEventArgs将当前 HttpContext 对象的 属性设置为User自定义IPrincipal对象。 如果未为 User 由 属性引用Context的 的 HttpContext 属性指定值,则 DefaultAuthenticationModule 会将 的 HttpContext 属性设置为UserGenericPrincipal不包含用户信息的对象。

DefaultAuthentication_OnAuthenticate 事件在 AuthenticateRequest 事件之后和 事件之前AuthorizeRequest引发。 如果某个 authorization 节依赖于用户名来拒绝或允许访问应用程序,则修改 User 当前 HttpContext 属性可能会影响应用程序的行为。 在配置中指定 authorization 节时,请确保考虑在 DefaultAuthentication_OnAuthenticate 事件期间设置的用户名。

适用于

另请参阅