Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ClaimsPrincipal in Asp.Net 5 (Mvc 6)

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!:)

like image 775
mikal Avatar asked Nov 09 '22 13:11

mikal


1 Answers

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.

like image 58
Matt DeKrey Avatar answered Nov 29 '22 20:11

Matt DeKrey