I would like to extend IPrincipal in asp.net to allow me to get the usertype that I will define. I would like to make it possible to do this in a controller
string type = User.UserType
then in my extension method i will have a method like
public string UserType()
{
// do some database access
return userType
}
how can I do this? is it possible? Thanks!
Here is an example custom class that implements IPrincipal. This class includes a few extra methods to check for role affiliation but shows a property named UserType per your requirements.
Enjoy!
You can make an extension method:
Sure. Make your class implements IPrincipal:
Extension method:
Basically, no. You can implement
IPrincipal
with a class such asMyPrincipal
, and that class can have aUserType
property, but you'd have to access the instance through a reference of its own type in order to reach it, not through the interface reference.edit
An extension method could work, but only if you're absolutely certain you'll never call it on something that implements
IPrincipal
but isn't an instance of your own class.