刷新 ASP.NET 角色提供程序
简单的问题...
假设我有一个 ASP.NET 站点,它使用[自定义] RoleProvider,
有什么方法可以让我以某种方式“刷新”提供程序,而无需强制用户注销网站并重新登录?
我正在寻找类似于虚构方法的东西
Roles.Refresh()
具体来说,我正在寻找这种方法,如果管理员更改用户的角色,用户会话可能会每 10 分钟或其他时间刷新一次。
simple question...
Given I have an ASP.NET site, which uses a [custom] RoleProvider,
Is there any way in which I can somehow "refresh" the provider without forcing the user to log out of the site and log back in?
I'm looking for something that would be akin to a fictional method
Roles.Refresh()
Specifically, I am looking at this for if an administrator changes a user's roles, the user sessions could maybe refresh themselves every 10 minutes or something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我假设您的
web.config
中有类似的内容:角色为 缓存在 cookie 中,因此您可以通过 删除 cookie。这个方法对我有用。我添加了
cookieName
属性,这样我就不会依赖 asp.net 的默认值。不过,对于您的场景,您可能只需将 cookieTimeout 属性设置为合理的值即可完成。当然,此方法不会立即更新角色。删除 cookie 后,它们将在下一个页面加载时更新。
I assume you have something like this in your
web.config
:The roles are cached in a cookie , so you can force them to refresh by deleting the cookie. This method worked for me. I added the
cookieName
attribute so that I don't rely on asp.net's default. For your scenario, though, you may be able to just set thecookieTimeout
attribute to something reasonable and be done with it.This method won't update the roles immediately, of course. They will be updated on the next page load after you delete the cookie.
刷新只需删除 cookie:
对于 C#:
Roles.DeleteCookie();
// Works as Roles.Refresh()Refresh just need to delete the cookie:
For C#:
Roles.DeleteCookie();
// Works as Roles.Refresh()如果您不想使用 cookie,您可以使用 Session 对象来缓存角色。
像这样:
当您需要更新该用户的角色时,您可以这样做
If you don't want to use cookies you can use Session object to cache the roles.
like this:
When you need to update the roles for this user you can do
取决于所使用的自定义角色提供程序。
只需对每个请求调用“更新我的角色”功能? (不好的方法,但至少你确定更新它)
depend on the custom role provider used.
Just call a "update my role" function on every request? (bad way but at least your sure to update it)
这些角色缓存在 cookie 中(当然是加密的)。最简单的解决方案是在 web.config 文件中禁用缓存。你会失去一些性能。
否则,您必须以某种方式重新发送身份验证 cookie。一个主要问题是许多浏览器不接受使用 post 方法重定向的 cookie。
对我有用的另一个解决方案:
1)在 aspx 方法中,注销用户并将用户名存储在会话中
//将用户添加到角色审阅者并刷新票证
2)在登录页面中,如果用户名存储在,则再次登录用户会议
The roles are cached in a cookie (encrypted of course). The simplest solution will be to disable caching in the web.config file. You will loose some performance.
Else you must somehow resend the auth cookie. One major problem is that many browsers will not accept cookies on redirects with method post.
The other solution that worked for me:
1) In a aspx methodod log the user out and store the username in the session
//Add User to role reviewer and refresh ticket
2) In the loginpage sign the user in again if username is stored in session