本文介绍了的Global.asax事件:Application_OnPostAuthenticateRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Application_OnPostAuthenticateRequest
事件的Global.asax
获得
)同时我已经作出了自定义主体类来获取用户详细信息和角色和权限角色和身份验证的用户的权限。
B):要获得这对用户保持相同的一些信息。
无效Application_OnPostAuthenticateRequest(对象发件人,EventArgs的发送)
{ //获取当前用户参考
IPrincipal的objIPrincipal = HttpContext.Current.User; //如果我们正在处理一个身份验证的窗体身份验证请求
如果((objIPrincipal.Identity.IsAuthenticated)及及(objIPrincipal.Identity.AuthenticationType ==形式))
{
CustomPrincipal objCustomPrincipal =新CustomPrincipal();
objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name);
HttpContext.Current.User = objCustomPrincipal;
CustomIdentity CI =(CustomIdentity)objCustomPrincipal.Identity;
HttpContext.Current.Cache [CountryID] = FatchMasterInfo.GetCountryID(ci.CultureId);
HttpContext.Current.Cache [WeatherLocationID] = FatchMasterInfo.GetWeatherLocationId(ci.UserId);
= Thread.CurrentPrincipal中objCustomPrincipal;
}
}
我的问题是:
- 此事件为每个请求每次。因此,对于每个请求code执行?
- 我的做法正确与否?
- 是正确添加HttpContext.Current.Cache在这种情况下,还是应该将其移动到在session_start
解决方案
- 是此事件的每个请求
- 是的,你可以使用这个事件来获得身份验证的用户信息
- 否,不要用
HttpCurrent.Current.Cache
来存储用户特定的信息作为缓存是很常见的所有用户和你会得到的冲突。使用HttpContext.Current.Session
,而不是因为这将是特定于用户。
I am using Application_OnPostAuthenticateRequest
event in global.asax
to get
a) Roles and permissions of authenticated user also i have made my custom principal class to get user detail and roles and permission.
b) To get some information which remain same for that user.
void Application_OnPostAuthenticateRequest(object sender, EventArgs e)
{
// Get a reference to the current User
IPrincipal objIPrincipal = HttpContext.Current.User;
// If we are dealing with an authenticated forms authentication request
if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "Forms"))
{
CustomPrincipal objCustomPrincipal = new CustomPrincipal();
objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name);
HttpContext.Current.User = objCustomPrincipal;
CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity;
HttpContext.Current.Cache["CountryID"] = FatchMasterInfo.GetCountryID(ci.CultureId);
HttpContext.Current.Cache["WeatherLocationID"] = FatchMasterInfo.GetWeatherLocationId(ci.UserId);
Thread.CurrentPrincipal = objCustomPrincipal;
}
}
My question is the following:
- This event fires every time for every request. Hence for each request the code execute?
- My approach is right or not?
- Is it right to add HttpContext.Current.Cache in this event or we should move it to Session_Start
解决方案
- Yes this event fires for every request
- Yes you can use this event to get information for the authenticated user
- No, don't use
HttpCurrent.Current.Cache
to store user specific information as the cache is common for all users and you will get conflicts. UseHttpContext.Current.Session
instead as this will be specific to the user.
这篇关于的Global.asax事件:Application_OnPostAuthenticateRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!