0

在我的 ASP.net MVC 项目中,我有(以及其他角色)版主和用户。我想为版主提供“以用户身份查看当前页面”的选项。

我的方法是创建一个 ActionFilterAttribute 并重载 OnActionExecuting 和 OnResultExecuted,然后为给定用户呈现页面。

第一个想法是处理角色:

OnActionExecuting {
  ... //various checks, if role exist, if user want to switch
  var tempRoles = Roles.getRolesForUser(user);
  filterContext.HttpContext.Items["tempRole"] = tempRoles;
  Roles.RemoveUserFromRoles(user, tempRoles)
  Roles.AddUserToRole(user, targetRole);
}

接着

OnResultExecuted {
//if switched view
{
   Roles.RemoveUserFromRole(user,targetRole)
   Roles.AddUserToRoles(filterContext.HttpContext.Items["tempRole"])
}

这行得通,但在最坏的情况下,角色消失了,所以我不想碰它们......

我的第二个想法是创建一个虚拟用户,将他添加到用户角色中,使用 FormsAuthentication.SetAuthCookie(dummyUser, true) 将版主登录到此帐户并还原 OnResultExecuted 中的所有内容,因此在最坏的情况下,用户处于 dummyRole 中(其中他可以注销)并且 dummyUser 在数据库中。

在调试和研究之后,我意识到 SetAuthCookie 需要重定向才能生效 - 所以它不能以这种方式工作。

问题:

  • 有没有办法强制 SetAuthCookie 在没有重定向的情况下生效
  • 任何其他建议/方法如何完成此“以其他用户身份查看页面”?
  • 如果我的第一个想法是唯一的解决方案,我如何使它万无一失?
4

1 回答 1

1

阿霍伊基督徒,

您可以装饰类 SqlRoleProvider 并将其添加到角色管理器。

请参阅示例角色提供程序实现:http: //msdn.microsoft.com/en-us/library/tksy7hd7%28v=vs.100%29.aspx

修饰的 SqlRoleProvider 可以覆盖 IsUserInRole 方法,从而实现模拟功能。

编辑:我添加了以下代码:

public class MyRoleProvider : SqlRoleProvider
{
    private static ConcurrentDictionary<string, string> impersonationList;

    public MyRoleProvider() : base()
    {
        impersonationList = new ConcurrentDictionary<string, string>();
    }

    public static void startImpersonate(string username, string rolename)
    {
        impersonationList.TryAdd(username,rolename);
    }

    public override string[] GetRolesForUser(string username) {
        if (impersonationList.ContainsKey(username))
            return new string[] { impersonationList[username] };
        else
            return base.GetRolesForUser(username);
    }

    public static void stopImpersonate(string username)
    {
        string rolename;
        impersonationList.TryRemove(username, out rolename);
    }
}
于 2013-07-25T12:01:48.280 回答