DefaultAuthenticationEventArgs(HttpContext) 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 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属性是否User为 null。 如果该属性为 null,则本示例将 User 当前 HttpContext 对象的属性设置为 GenericPrincipal 对象,其中 Identity 该对象的属性 GenericPrincipal 是 GenericIdentity 属性值为“default”的对象 Name 。
注释
在事件之前AuthorizeRequest引发DefaultAuthentication_OnAuthenticate事件。 因此,如果将当前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指定值,则DefaultAuthenticationModuleUser设置对象的属性HttpContextGenericPrincipal不包含用户信息。
DefaultAuthentication_OnAuthenticate事件在事件之后AuthenticateRequest和事件之前AuthorizeRequest引发。 如果你有一个 authorization 依赖于用户名拒绝或允许访问应用程序的分区,则修改 User 当前 HttpContext 属性可能会影响应用程序的行为。 确保在配置中指定authorization节时,将考虑在DefaultAuthentication_OnAuthenticate事件期间设置的用户名。