Global.asax事件:Application_OnPostAuthenticateRequest

时间:2009-07-18 11:04:51

标签: authentication asp.net-2.0

我在Application_OnPostAuthenticateRequest中使用global.asax事件来获取

a)经过身份验证的用户的角色和权限我也使用了自定义主体类来获取用户详细信息和角色及权限。

b)获取一些对该用户保持相同的信息。

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;
    }
}

我的问题如下:

  1. 每次请求都会触发此事件。因此,对于每个请求代码执行?
  2. 我的方法是对还是没有?
  3. 在此事件中添加HttpContext.Current.Cache是​​正确的还是我们应该将其移至Session_Start

1 个答案:

答案 0 :(得分:4)

  1. 是的,此事件会针对每个请求触发
  2. 是的,您可以使用此事件获取经过身份验证的用户的信息
  3. 不,不要使用 HttpCurrent.Current.Cache来存储用户特定信息,因为缓存对所有用户都是通用的,您将会遇到冲突。请改用HttpContext.Current.Session,因为这将特定于用户。