【问题标题】:asp.net make custom role provider (authorize) behave like the custom membership provider (authenticate) in expirationasp.net 使自定义角色提供者(授权)在到期时表现得像自定义成员提供者(身份验证)一样
【发布时间】:2011-05-28 00:09:11
【问题描述】:

我的表单身份验证工作正常,cookie 设置为过期 3 个月:

FormsAuthentication.Initialize();
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, this.txtUsername.Text, DateTime.Now
                    , DateTime.Now.AddMonths(3), true, string.Empty);

因此,即使 IIS 重新启动或项目重建发生,用户仍然会进行身份验证,直到他选择退出我们过去的 3 个月。

至于用户登录时的自定义角色提供者[授权部分] isValid() 我添加会话变量:

HttpContext.Current.Session.Add("userinfo", userInfo);

但我们知道会话在 web.config 更改、项目构建、IIS 重新启动或默认经过 20 分钟后过期。

我想要的只是让系统保存 Session["userinfo"] 与身份验证 [cookie] 相同,但当然没有在 cookie 中设置 userinfo,因为即使 userId 被认为是存储在 cookie 中的安全漏洞也是不安全的!

那么如何实现呢?我想将用户 ID 存储在 cookie 中但已加密,然后如果我发现会话已过期但用户仍然经过身份验证,我将从数据库重新加载 userInfo,但这种方法是否足够好或更好?以及如何将userInfo存储在authTicked in (string.Empty)上面的代码段中,以后可以访问,如何使用?

【问题讨论】:

  • 另外,我可以为单个会话变量设置自定义过期日期,尽管这不会解决 IIS 或项目构建问题!!!

标签: asp.net session forms-authentication authorization security


【解决方案1】:

好的,听起来没有人回应!所以我选择将 userId 存储在身份验证票的用户数据部分:

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, this.txtUsername.Text, DateTime.Now
                , DateTime.Now.AddMonths(3), true, UserInfo.UserId.ToString());

然后当我需要检查 userInfo 时,我使用以下属性:

public UserInformation UserInfo
    {
        get
        {
            if (Session["userinfo"] == null)
            {
                FormsIdentity id = (FormsIdentity)User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;
                long userId = Convert.ToInt64(ticket.UserData);
                Session["userinfo"]=new MySqlMembershipProvider().LoadUserInfo(userId);
            }
            return (UserInformation)Session["userinfo"];
        }
    }

就是这样。我想到了配置文件提供者,但我不喜欢从 db [9 个表结构] 获取用户权限然后将它们重新存储在会话表中的记录下 [就像在你自己周围循环一样],如果用户权限或首选项更新需要更多的数据库命中!

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    相关资源
    最近更新 更多