加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC在会话中保存令牌

发布时间:2020-12-16 06:31:52 所属栏目:asp.Net 来源:网络整理
导读:我正在调用一个服务,我需要在每次发出请求时传递用户的永久安全令牌. 为此,我已将此方法添加到我的基本控制器类: protected UserData getUsr(){ try { UserData usr = new UserData(); usr.SecurityToken = Session["secToken"].ToString(); MembershipUser
我正在调用一个服务,我需要在每次发出请求时传递用户的永久安全令牌.

为此,我已将此方法添加到我的基本控制器类:

protected UserData getUsr()
{
        try
        {
            UserData usr = new UserData();
            usr.SecurityToken = Session["secToken"].ToString();

            MembershipUser mvcUser = Membership.GetUser(HttpContext.User.Identity.Name);
            usr.Id = (int)mvcUser.ProviderUserKey;

            return usr;
        }
        catch (Exception ex)
        {
            log.Debug("Could not create usr object",ex);
            throw new Exception("Could not authenticate");
        }
    }

这里的问题是,有时User.Identity数据会超出会话数据,导致用户看到他们登录但随后他们的请求失败会发生奇怪的错误.

是否有更好的方法来存储此令牌/我可以存储它以便在User.Identity对象到期时它将过期吗?

此外,如果有人知道一些很好的基本理解示例/文档HttpContext和MVC授权过滤器将是伟大的.

解决方法

我会去表单身份验证cookie本身存储用户的安全令牌. FormsAuthenticationTicket类包含UserData属性,您可以在其中包含附加信息.

The value specified in the UserData property is included as part of
the authentication ticket cookie and,like the other ticket fields,is
encrypted and validated based on the forms authentication system’s
configuration.

这是一个article,描述了如何将其他信息存储到表单身份验证cookie.

This是一篇很重要的文章,解释了将更多数据存储到表单身份验证中. cookie以及如何阅读它.代码是用VB编写的,格式不正确.您必须向下滚动到故障单中的步骤4:存储其他用户数据.

这个thread将帮助您快速回答如何从cookie中读取UserData.

我会去创建一个自定义的ValueProvider,如here所述,它将从auth中读取安全令牌. cookie和动作参数的提要.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读