简体   繁体   English

在ASP.NET MVC中扩展HttpContext.User.Identity

[英]extend HttpContext.User.Identity in asp.net mvc

in my application i want to use the current logged in user details, in HttpContext.User.Identity there are 'AuthenticationType', 'IsAuthenticated' and 'Name', I want some more details from database when user logged in to the system. 在我的应用程序中,我想使用当前登录的用户详细信息,在HttpContext.User.Identity中有“ AuthenticationType”,“ IsAuthenticated”和“ Name”,当用户登录系统时,我希望从数据库中获取更多详细信息。 Is there any way to extend this identity class? 有什么方法可以扩展此标识类吗?

As Daniel noted in his comment you're going to need to create your own implementation of IPrincipal. 正如Daniel在评论中指出的那样,您将需要创建自己的IPrincipal实现。 I'm going to assume that you're using MVC5. 我将假设您正在使用MVC5。 If so, then you're going to need to implement an authentication filter which will allow you to set the principal for the request. 如果是这样,那么您将需要实现一个身份验证过滤器 ,该过滤器将允许您设置请求的主体。 This authentication filter would go to the database, retrieve the fields you have added, and populate your IPrincipal implementation. 此身份验证筛选器将转到数据库,检索您添加的字段,然后填充IPrincipal实现。 Finally, you would need to globally register that filter in your FilterConfig.cs file in App_Start so that it will be applied to every controller action in the application without you having to type it over and over again. 最后,您需要在App_Start的FilterConfig.cs文件中全局注册该过滤器,以便将其应用于应用程序中的每个控制器操作,而不必一遍又一遍地键入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM