Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 应用程序身份验证请求和表单身份验证身份验证之间有什么区别_C#_Asp.net_Webforms_.net 4.5 - Fatal编程技术网

C# 应用程序身份验证请求和表单身份验证身份验证之间有什么区别

C# 应用程序身份验证请求和表单身份验证身份验证之间有什么区别,c#,asp.net,webforms,.net-4.5,C#,Asp.net,Webforms,.net 4.5,我一直在尝试自定义ASP.NET表单身份验证,但让我困惑的是在哪里设置与用户关联的角色。在阅读各种教程时,我看到了使用应用程序\u AuthenticateRequest或表单身份验证\u OnAuthenticate的建议,其中唯一的区别是用户主体如何分配给用户 Context.User = userPrincipal; 及 下面是每个函数的已完成方法调用。这些功能是等效的,还是我应该注意到一些差异 protected void Application_AuthenticateReq

我一直在尝试自定义ASP.NET表单身份验证,但让我困惑的是在哪里设置与用户关联的角色。在阅读各种教程时,我看到了使用
应用程序\u AuthenticateRequest
表单身份验证\u OnAuthenticate
的建议,其中唯一的区别是
用户主体
如何分配给
用户

Context.User = userPrincipal;

下面是每个函数的已完成方法调用。这些功能是等效的,还是我应该注意到一些差异

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            string[] roles = authTicket.UserData.Split(';');
            GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(authTicket.Name), roles);
            Context.User = userPrincipal;
        }
    }


    protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
    {
        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            string[] roles = authTicket.UserData.Split(';');
            GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(authTicket.Name), roles);
            e.User = userPrincipal;
        }
    }

这些看起来是一样的,可能会产生或多或少相同的结果。但是,有一些关键的区别,它们与Asp.net管道以及调用和触发各种事件的顺序有关

当asp.net初始化时,它将
FormsAuthentication\u onAuthentication()
处理程序挂接到
应用程序\u AuthenticateRequest
事件。因此,当调用
AuthenticateRequest
时,它将遍历处理程序链并按顺序调用它们

碰巧的是,asp.net为此配置的第一个模块是
FormsAuthentication
,这意味着将首先调用
FormsAuthentication\u OnAuthenticate()
处理程序,然后调用可能也配置的任何自定义模块,最后调用global.asax中配置的任何模块

基本上,这都是关于事物被调用的顺序

那么问题的答案是,它们之间有什么区别。。它们是两个不同的处理程序,在同一事件的身份验证管道中的不同点调用。

在大多数情况下,使用哪一种可能无关紧要,但在某些情况下可能会。。例如,如果您在
FormsAuthentication\u OnAuthenticate()
方法中执行了此操作,则链中的后续处理程序可能会覆盖您使用其自身设置所做的操作

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            string[] roles = authTicket.UserData.Split(';');
            GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(authTicket.Name), roles);
            Context.User = userPrincipal;
        }
    }


    protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
    {
        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
            string[] roles = authTicket.UserData.Split(';');
            GenericPrincipal userPrincipal = new GenericPrincipal(new GenericIdentity(authTicket.Name), roles);
            e.User = userPrincipal;
        }
    }