User property, utility and possible Displays

Asked

Viewed 114 times

1

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

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

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

That property User is apparently the same as in Asp.Net Webforms, of which I know nothing either.
The estate User is the type Iprincipal and that implements a property of the type Iidentity and a method bool IsInRole(string role).

In turn, Claimsprincipal inherits from Iprincipal, suggesting why the cast on the property Currentuser it is possible.
But the property even returns is a type Appuserprincipal which, despite the name, inherits from Claimsprincipal and apply some properties:

public class AppUserPrincipal : ClaimsPrincipal
{
    public AppUserPrincipal(ClaimsPrincipal principal): base(principal) { }

    public string Name {
        get { return this.FindFirst(ClaimTypes.Name).Value; }
    }

    public string Country {
        get { return this.FindFirst(ClaimTypes.Country).Value; }
    }
}

Well, all this so far was just to show a little bit where I walked and then facilitate the understanding of my doubts.
Doubts that because I’m still a total Newbie in the world of languages Microsoft and the environment .Net. In addition to not having understood yet the structure of the classes and interfaces of the Asp.Net Identity and how he works. Before I only programmed for desktop with Delphi and many doubts were solved easily by being able to access the fonts of the classes, methods and functions and see how they were written.

Anyway, I’m not one to use things with constancy and tranquility that I don’t understand. So those are the doubts:

  1. What is the property User of Controller of Asp.Net Mvc and, it is the same thing as that of a Webform? What is its purpose?
  2. The estate User stores logged-in user information? Stores in cookie or session?
  3. If I inherit my Identity user can get your instance through a cast? Or is it not necessary because there would be another way?
  4. The question about the User property storing a user instance comes, too, from the method bool IsInRole(string role) of the interface Iprincipal. How do I know the roles a user has will be there? How and where this is done?

Stay on it for now so we don’t make the answers too big.

1 answer

2


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

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

Does the User property store logged-in user information? Store as a cookie or session?

Depends on the implementation. The native uses both.

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

Yes, you can. You’d just have to implement your CustomPrincipal, implementing the interfaces required by an object it inherits from IPrincipal.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.