Custom ClaimsPrincipal in Asp.Net 5 (Mvc 6)

情到浓时终转凉″ 提交于 2019-12-12 09:43:14

问题


How can I override the default Context.User of type System.Security.Claims.ClaimsPrincipal in Asp.Net 5 (MVC-6)?

I would like to use my customized User-type, so that it's accessible in Controllers (HttpContext.User), as well as in the Razor views (@User.)

Any help is much appreciated. Thanks!:)


回答1:


The basic answer is don't - it's generally a bad idea to try to implement your own security code; there's lots of options out there that will save you a lot of time up front, and save you a lot of headaches on the back side, too.

The other answer is that you can't - it's built in to the new framework from the beginning.

This is what a User POCO model is for. The Identity framework has operated this way (I think since the beginning), and it mirrors OAuth and most other authentication/authorization systems. It's an incredibly flexible and efficient model.

Instead what I would recommend doing is build your own ClaimTypes and use those in addition to the ones built in to the framework. Depending on how you're authenticating the user, you should be able to add them when you would create the IPrincipal, anyway.



来源:https://stackoverflow.com/questions/30292985/custom-claimsprincipal-in-asp-net-5-mvc-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!