Asp.net core 2.0 authentication with cookies

Asked

Viewed 380 times

1

I am making 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 managed to do, already checking the data in the database, but I came up with a question, and I did not find the answer, as I do to get the user data logged in after authentication?

saw that in the code part below it stores some user data, so how do I take 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 take the values set in Claimtypes.Nameidentifier and Claimtypes.Name, how would you do that? or do I need to store this data in session?

Thank you!

2 answers

0

Hello, a quick search on Claims you’ll find lots of things.

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

try placing this code on the Index page:

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

0

Thus:

@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 (Scroll):

@User.IsInRole("Administrador")

Browser other questions tagged

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