在每个请求上获取用户数据
我认为我的问题很简单。 我正在做一个 ASP.NET MVC 项目。 这是一个需要用户始终登录的项目。 我可能需要母版页中当前用户的信息,如下所示; “你好,马克 - 您已登录!”。
但是如果我在视图中需要相同的信息怎么办? 或者在我的服务层中进行一些验证?
那么如何确保这些信息在我需要的时候、在我需要的地方可用呢?
My problem is quite simple - I think. I'm doing an ASP.NET MVC project. It's a project that requires the user to be logged in at all time. I probably need the current user's information in the MasterPage, like so; "Howdy, Mark - you're logged in!".
But what if I need the same information in the view? Or some validation in my servicelayer?
So how to make sure this information is available when I need it, and where I need it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要多少用户信息? 您始终可以访问 Thread.Current.Principal 并获取用户名 - 并可能使用它在数据库中查找有关用户的更多信息。
或者,如果您真的真的一直需要一些信息,您可以实现从 IPrincipal 派生的您自己的自定义主体(这真的不是什么大问题!),并在其中添加这些信息,当用户登录时,创建 MyCustomPrincipal 的实例并将其附加到当前线程。 然后,它将随时随地可用。
马克
How much user information do you need? You can always access the Thread.Current.Principal and get the user's name - and possibly use that to look up more info on the user in a database.
Or if you really really really need some piece of information at all times, you could implement your own custom principal deriving from IPrincipal (this is really not a big deal!), and add those bits of information there, and when the user logs in, create an instance of the MyCustomPrincipal and attach that to the current thread. Then it'll be available anywhere, everywhere, anytime.
Marc
我遇到了完全相同的问题,但尚未找到满意的答案。 我们探索过的所有选项都存在各种问题。 在您提到的具体示例中,您显然可以将该数据存储在会话中,因为这适用于该示例。 可能还有其他情况,我们已经遇到过,这可能不起作用,但像这样的简单用户信息在会话中就可以了。
我们刚刚设置了一个 BaseController,它负责确保每个视图的信息始终设置且正确。 根据您处理身份验证等的方式,您将始终在 HttpContext.User.Identity.Name 中获得一些可用的用户数据。 也可以参考一下。
I've had exactly the same issue, and have yet to find a satisfactory answer. All the options we've explored have had various issues. In the specific example you mention, you could obviously store that data in the session, as that would work for that example. There may be other scenarios, that we've had, where that may not work, but simple user info like that would be fine in the session.
We've just setup a BaseController that handles making sure that info is always set and correct for each view. Depending on how you're handling authentication, etc, you will have some user data available in HttpContext.User.Identity.Name at all times. Which can also be referenced.
构建模型的层次结构并将共享信息放入基本模型中。 这样,它将可用于任何视图或部分视图。
当然,由于 Web 应用程序不是持久性的,因此必须在每次请求时检索它。
Build a hierarchy of your models and put the shared information in the base model. This way it will be available to any view or partial view.
Of course it has to be retrieved on each request since web applications are not persistent.
您应该将其存储在 Session 中,并通过自定义 ModelBinder 将其检索到控制器中。
You should store this in Session and retrieve it into your controllers via a custom ModelBinder.
不确定我是否明白你想问的问题,但如果你正在寻找诸如身份验证和基于角色的授权之类的东西,实际上 ASP.net 提供了一个很好的框架来工作/开始。
这篇文章(还有第二部分)是我最近发现的,阅读有关提供者模式的最佳起点,这有助于理解 ASP.net 的底层身份验证框架。 请务必阅读 msdn 中的membershipProvider 类和 RoleProvider 类,它们一起构建了一个关于最基本的基于角色的身份验证的优秀框架(如果您对它们提供的功能感到满意,您甚至不需要代码数据访问部分,所有内容都在默认实现中提供!)
PS:也请查看 Context.Users 属性! 它存储当前经过身份验证的用户信息。
Not sure if I get what you want to ask, but if you are looking for things like authentication and role-based authorization, actually ASP.net is providing a great framework to work on/start with.
This article (with also 2nd part) is something I recently discovered and read about which is really good start with the provider-pattern which help to understand the underlying authentication framework of ASP.net. Be sure to read about the membershipProvider class and the RoleProvider class in msdn also, they together make a great framework on most basic role-base authentication to work with (if you are comfortable with the function they provided, you even don't need to code data-access part, all are provided in the default implementation!)
PS: Check out Context.Users property too! It stores the current authenticated user information.
HttpContext.Current.Users.Identity 返回当前用户的信息。 尽管我不确定当您进行网络服务调用时它是否会隐式传递。
HttpContext.Current.Users.Identity returns the current user's information. Though I am not sure whether it gets passed implicitly when you make a webservice call.