用户身份如何?在应用程序生命周期中设置的主体

时间:2021-11-16 06:47:58

In ASP.NET (MVC and WebAPI), how is the User Identity object initialized with the data from the FormsAuthentication cookie?

在ASP.NET(MVC和WebAPI)中,如何使用FormsAuthentication cookie中的数据初始化User Identity对象?

What I want to do is to use 2 types of authentication, cookie based and a custom header based one. Since the AuthorizeAttribute class only checks the User.Identity.IsAuthorized() method and uses no code specific to FormsAuthentication, then that means all I have to do is to manually setup the User.Identity object, whether in the global.asax or in a DelegatingHandler.

我想要做的是使用两种类型的身份验证,基于cookie和基于自定义标头。由于AuthorizeAttribute类只检查User.Identity.IsAuthorized()方法并且不使用特定于FormsAuthentication的代码,这意味着我所要做的就是手动设置User.Identity对象,无论是在global.asax还是在DelegatingHandler。

So, how do I setup User.Identity? How does the framework automatically set it up using FormsAuthentication?

那么,我该如何设置User.Identity?框架如何使用FormsAuthentication自动设置它?

2 个解决方案

#1


1  

Here is a related answer.

这是一个相关的答案。

Identity can be set in a handler, in a module, or in Global.asax. You can modify it at any point in the request lifecycle, but usually the best place to do it is in PostAuthenticateRequest. At this point, FormsAuthentication has done it's work and you can augment or replace the HttpContext.Current.User.

可以在处理程序,模块或Global.asax中设置标识。您可以在请求生命周期的任何时候对其进行修改,但通常最好的地方是在PostAuthenticateRequest中。此时,FormsAuthentication已完成它的工作,您可以扩充或替换HttpContext.Current.User。

#2


0  

ASP.NET MVC Controller has a Controller.User but Web API does not have one. It is currenty being requested to be put in.

ASP.NET MVC Controller有一个Controller.User但Web API没有。目前要求被投入使用。

#1


1  

Here is a related answer.

这是一个相关的答案。

Identity can be set in a handler, in a module, or in Global.asax. You can modify it at any point in the request lifecycle, but usually the best place to do it is in PostAuthenticateRequest. At this point, FormsAuthentication has done it's work and you can augment or replace the HttpContext.Current.User.

可以在处理程序,模块或Global.asax中设置标识。您可以在请求生命周期的任何时候对其进行修改,但通常最好的地方是在PostAuthenticateRequest中。此时,FormsAuthentication已完成它的工作,您可以扩充或替换HttpContext.Current.User。

#2


0  

ASP.NET MVC Controller has a Controller.User but Web API does not have one. It is currenty being requested to be put in.

ASP.NET MVC Controller有一个Controller.User但Web API没有。目前要求被投入使用。