User property, utility and possible casts

1

For educational purposes, I was looking at the User property of the Asp.Net MVC Controller class.

I saw a example , very interesting , that a base class for controllers has been implemented and it has the CurrentUser property:

public abstract class AppController : Controller
{
    public AppUserPrincipal CurrentUser {
        get { return new AppUserPrincipal(base.User as ClaimsPrincipal); }
    }
}

This User property is apparently the same as that in Asp.Net Webforms , which I know nothing about. The User property is of type IPrincipal and implements a property of type IIdentity and a bool IsInRole(string role) method.

ClaimsPrincipal inherits from IPrincipal , implying why cast on the property > CurrentUser is possible. But the property even returns is an type

asked by anonymous 25.09.2014 / 15:24

1 answer

2

What is the User property of the Asp.Net Mvc Controller, and is it the same as a WebForm? What is the purpose of it?

User is an interface object IPrincipal . A Principal object is intended to identify an authenticated user in an ASP.NET application. Microsoft wrote a much more detailed text that can be read here .

Does the User property store the logged-in user information? Do you store in cookie or session?

Depends on implementation. The native uses both .

If I inherit my Identity user can I get their instance through a cast? Or is it not necessary because there would be another way?

Yes, you can. You would only have to implement your CustomPrincipal , implementing the interfaces required by an object that inherits from IPrincipal .

The question about the User property storing a user instance also comes from the bool IsInRole (string role) method of the IPrincipal interface. How do I know which roles a user has will be there? How and where is it done?

This is done through RoleManager , which is designed to respond to the IPrincipal interface for Roles.

    
25.09.2014 / 21:17