使用表单身份验证的ASP.Net网站在回发时引发安全异常

使用表单身份验证的ASP.Net网站在回发时引发安全异常,asp.net,security,forms-authentication,securityexception,principalpermission,Asp.net,Security,Forms Authentication,Securityexception,Principalpermission,我为我的ASP.net web应用程序使用了自定义的基于角色的表单身份验证安全性。角色、用户从sql server获取。该框架针对4.0版 我使用了来自的代码 在登录时创建FormsAuthenticationTicket,然后我编写了在应用程序\u PreRequestHandlerExecute方法中创建GenericPrincipal对象的代码 string cookieName = FormsAuthentication.FormsCookieName; HttpCookie

我为我的ASP.net web应用程序使用了自定义的基于角色的表单身份验证安全性。角色、用户从sql server获取。该框架针对4.0版

我使用了来自的代码 在登录时创建FormsAuthenticationTicket,然后我编写了在应用程序\u PreRequestHandlerExecute方法中创建GenericPrincipal对象的代码

  string cookieName = FormsAuthentication.FormsCookieName;
   HttpCookie authCookie = Context.Request.Cookies[cookieName];
   if (authCookie == null) return;
   FormsAuthenticationTicket authTicket;
  try
  {
    authTicket = FormsAuthentication.Decrypt(authCookie.Value);
  }
   catch (Exception ex)
   {

      throw;
   }
   if (authTicket == null)
    throw new Exception("");

   FormsIdentity id = new FormsIdentity(authTicket);
   if(HttpContext.Current.Session  != null)
   {

   GenericPrincipal principal = new GenericPrincipal(id, new String[]{ "Manager"});

   System.Threading.Thread.CurrentPrincipal = principal;
   }
我已将PrincipalPermission属性设置为重定向页面的page_load方法

[PrincipalPermissionAttribute(SecurityAction.Demand, Role="Manager")]
protected void Page_Load(object sender, EventArgs e)
{

}
我在这个页面中添加了一个按钮,用来模拟回发。第一次加载时,不会抛出任何安全异常,但如果我单击按钮,会抛出一个安全异常,说明“主体权限请求被拒绝”

但是,如果我将应用程序池设置为classic,这是不可复制的

只有当应用程序池设置为集成模式时,它才会失败,这有什么原因吗

GenericPrincipal principal = new GenericPrincipal(id, new String[]{ "Manager"}); 
HttpContext.Current.User = principal;
System.Threading.Thread.CurrentPrincipal = HttpContext.Current.User;
这就解决了这个问题,不知道为什么只有当管道被设置为integrated并且在没有此代码的情况下在classic中工作时才需要这样做

GenericPrincipal principal = new GenericPrincipal(id, new String[]{ "Manager"}); 
HttpContext.Current.User = principal; 
System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;

这就解决了这个问题,不知道为什么只有当管道被设置为“集成”并且在没有此代码的情况下以经典方式工作时才需要这样做。

GenericPrincipal=new GenericPrincipal(id,new String[]{“Manager”});HttpContext.Current.User=主体;System.Threading.Thread.CurrentPrincipal=System.Web.HttpContext.Current.User;这就解决了这个问题,不知道为什么只有当管道被设置为integrated并且在经典中没有此代码时才需要这样做。
GenericPrincipal principal = new GenericPrincipal(id, new String[]{ "Manager"}); 
HttpContext.Current.User = principal; 
System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;