动态更改IPrincipal的值

时间:2015-02-26 18:06:24

标签: asp.net-mvc

我在我的mvc网站上有一个自定义角色管理器,而且,我需要在当前用户上下文中更改一些值,为此,我尝试了这个:

    public JsonResult AlteraAsConfiguracoes(int id)
    {
        var empresa = Code.DataAccess.Repository.EmpresaDataRepository.GetAllEmpresasComboBox().empresasAdmin[id];
        HttpContext.User.ToCustomPrincipal().NomeDaEmpresa = empresa.Nome;
        HttpContext.User.ToCustomPrincipal().EmpresaId = empresa.EmpresaAdminId;
        return Json(new { sucesso = true }, JsonRequestBehavior.AllowGet);
    }

我在global.asax中注册用户的方法是:

   protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
   {
        HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

            CustomPrincipalSerializeModel serializeModel = JsonConvert.DeserializeObject<CustomPrincipalSerializeModel>(authTicket.UserData);
            CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
            newUser.UserId = serializeModel.UserId;
            newUser.EmpresaId = serializeModel.EmpresaId;
            newUser.Nome = serializeModel.Nome;
            newUser.SobreNome = serializeModel.SobreNome;
            newUser.roles = serializeModel.roles;
            newUser.NomeDaEmpresa = serializeModel.NomeDaEmpresa;

            HttpContext.Current.User = newUser;
        }
    }

但是,不工作......

我怎么能做到这一点?

聚苯乙烯。如果需要更多我的登录系统的代码,请问我放在这里。

0 个答案:

没有答案