asp.net core 2.0 authentication with cookies

1

I'm doing an authentication using cookies following the examples

Custom Authentication in ASP.Net-Core

Creating a simple login in ASP.NET Core 2 using Authentication and Authorization (NOT Identity)

I was able to do, already checking the data in the database, but I had a question, and I did not find the answer, how do I get the user data logged in after authentication?

I saw that in the code part below it stores some user data, so how do I get this data for example the user code to show only the records related to it?

identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginData.Username));
identity.AddClaim(new Claim(ClaimTypes.Name, loginData.Username));

For example, on the user data editing page, you would need to get the values set in ClaimTypes.NameIdentifier and ClaimTypes.Name , how would you do it? or do I need to store this data in session?

Thank you!

    
asked by anonymous 12.06.2018 / 14:53

2 answers

0

Hello, a quick search on Claims will find you a lot.

How do you follow the tutorial: Creating a simple login in ASP.NET Core 2 using Authentication and Authorization (NOT Identity),

Try putting this code in the Index page:

<p>@User.Identity.Name</p>
<ul>
    @foreach (var claim in User.Claims)
    {
        <li>@claim.Type : @claim.Value</li>
    }
</ul>
    
06.08.2018 / 17:44
0

This way:

@User.Identity.Name

If you want to know if the user is authenticated:

@User.Identity.IsAuthenticated

If you want to know if the user belongs to a profile type (Role):

@User.IsInRole("Administrador")
    
21.09.2018 / 16:56