我无法通过cookie加载用户角色的页面

时间:2017-01-05 16:40:06

标签: c# forms-authentication saml cas user-roles

我已经在iis7上采用了经过广告验证的网站,并将其迁移到iis 8.5。在此过程中,我尝试切换到表单身份验证。并非网站上的每个页面都是一个aspx页面 - 因为它是一个Intranet站点,那里有静态内容和文件 - 但基本的表单身份验证已经有效,因为我将应用程序池设置为集成模式并将web.config设置为使用表单身份验证。我非常高兴的是,如果您尝试加载pdf这样做,它将确保您首先登录到cas服务器。

我的问题在于角色 - 我已经在登录页面中设置了cookie以在其中拥有角色(我从CAS属性中提取),当我在该页面上执行User.IsInRole()时,它说是的,这个人有角色。但是如果我移动到一个新的页面,不管是不是aspx,那么该人仍然经过身份验证,但没有该角色。我试图避免使用角色管理器,因为我不想处理提供程序(cas服务器正在处理身份验证部分),但如果必须,我将使用角色管理器。到目前为止,我运气不好。我认为这些角色不是从cookie中加载的,因为我把它放在一起(User.IsInRole(" staff"))?" True":" False&# 34 ;;如下所示在主页面中行,但在登录页面上它返回true,在主页面上返回false。

我会附加我的网络配置和我的登录页面代码的相关部分。

Web配置(仅宣传文件夹使用角色atm):

2jpeg.exe -src "c:\files\myfile.docx" -dst "c:\files" -oper Resize size:"100 200" fmode:fit_width -options pages:"1" scansf:no overwrite:yes template:"{Title}_thumb.jpg" silent:yes

登录页面代码的相关部分(重定向被注释掉,因此我可以看到他们的角色):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <modules>
      <!-- Re-add auth modules (in their original order) to run for all static and dynamic requests -->
      <remove name="FormsAuthentication" />
      <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
      <remove name="DefaultAuthentication" />
      <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
      <remove name="RoleManager" />
      <add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
      <remove name="UrlAuthorization" />
      <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
    </modules>
  </system.webServer>
  <system.web>
    <authentication mode="Forms" />
    <authorization>
      <deny users="?" />
      <allow users="*" />
    </authorization>
  </system.web>
  <location path="announce">
    <system.web>
      <authorization>
        <allow roles="staff, faculty" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
</configuration>

1 个答案:

答案 0 :(得分:0)

我不想回答我自己的问题,但经过一周的搜索和阅读教程,我得到了解决方案。

上述问题中的cookie和表格认证都很好;角色没有被加载的原因是因为我的global.asax中没有PostAuthenticateRequest(实际上根本没有global.asax)。 This question最有帮助,不是来自接受的答案,而是来自P.Hoxha的附加项目。其中有0投票,但我投了赞成票。 Microsoft教程here也有助于更正我投票的响应中无效的内容。

添加global.asax通过捕获PostAuthenticateRequest并运行以下代码来加载角色来解决问题:

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Threading" %>

<script runat="server">
void Application_OnPostAuthenticateRequest(object sender, EventArgs e) {
  HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
  if (authCookie == null || authCookie.Value == "") return;
  FormsAuthenticationTicket authTicket;
  try {
    authTicket = FormsAuthentication.Decrypt(authCookie.Value);
  } catch {
    return;
  }

  // retrieve roles from UserData
  string[] roles = authTicket.UserData.Split('|');

  if (Context.User != null) Context.User = new GenericPrincipal(Context.User.Identity, roles);
}
</script>