How to get authenticated user identity name in asp net web api

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

All ASP .NET Web API Text Articles and Slides

All ASP .NET Web API Videos

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

We want to display the logged in username on the web page

success: function (response) {
},

[span id="spanUsername" class="text-muted"][/span]

How to get logged in user identity details in ASP.NET Web API controller
From the ASP.NET Web API controller use the User.Identity object to retrieve user details

User.Identity.IsAuthenticated - Returns true or false depending on whether the user is authenticated
User.Identity.AuthenticationType - Authentication Type used
User.Identity.Name - Logged in username

RequestContext.Principal.Identity.IsAuthenticated - Returns true or false depending on whether the user is authenticated
RequestContext.Principal.Identity.AuthenticationType - Authentication Type used
RequestContext.Principal.Identity.Name - Logged in username

Instead of using User.Identity, we can also use RequestContext.Principal.Identity

Now let us see what these properties return if we are not logged in.
1. Add a new Empty Web API 2 Controller to the EmployeeService project. Name it TestController.
2. Copy and paste the following code in TestController.

using System.Web.Http;
namespace EmployeeService.Controllers
{
public class TestController : ApiController
{
public string Get()
{
return "Hello from TestController";
}
}
}

3. Set a breakpoint on the Get() method in TestController. Run the application in Debug mode and navigate to /api/Test. When the execution pauses at the breakpoint, open Immediate Window and type the above properties one by one and press enter.

Since we are not logged in, notice IsAuthenticated property returns false. Since we are not authenticated AuthenticationType and Name properties return NULL.
Рекомендации по теме
Комментарии
Автор

Sir your tutorials are really awesome.
please make some videos on
1) Customize inbuilt identity framework .
2) Role based authorization.
3) Intercepting all incoming and outgoing requests.
4) Your valuable guidelines on Performance.

neerajjadon
Автор

Can you guide the authentication and authorization using roles..! thank you

trancongsang
Автор

Sir really big fan of you, looking forward to see your videos on "Token base authentication and authorization using roles in ANGULAR JS with ui router"

rashidaslam
Автор

thank you Sir for your big effort.. GBU

rahmiul
Автор

cool...But i have two doubts,

1. if any third party is accessing our web API then how can we come to know identity??

2.instead of Microsoft inbuilt auto generated identity table can we used our own created table for authorization??

thanking you

ramchandrathakkar
Автор

Thank you so much for this tutorial, it really helps, I have learned a lot.

By the way, can you make a tutorial for ASP.NET core? I have a lot of problem with it, especically about Identity.

Pblaze
Автор

Is this secured? because we are storing data in browser, what if the hacker tries to break the application?

feelgoodcontent
Автор

Thank you so much Kudvenkat for the great jo you are doing. I don't know you could record a tutorial about publish web application to a remote server using Publish Tool.

Thank you in advance

mohkhatari
Автор

hello sir...will u plz do a video about how to convert website to web application in asp.net....plz help

kavithavishwanathen
Автор

Hi Can you make video on 3rd party authentication using angular js

rakeshkaddi
Автор

Always great tutorial....Thanks a lot..:-)

kishan.r.khamitkar
Автор

Hi Can you Please make video on Microsoft login as well.

rakeshkaddi
Автор

Please create tutorial authorization using roles thank you sir

harisahmed
Автор

ive made the same api and im trying to consume it by adding another project into the solution. Yet i am not able to register even tho cors is enabled but i cant seem to hit the ./token

salehadnan
Автор

Hello sir, please upload videos for Web api with AngularJs and role based login. Thank You!!!

subratkar
Автор

error accure User.Identity.IsAuthenticated
false. please help how to fix this error.

qaisarali