Topics
Published on December 13, 2016 by kudvenkat
Want create site? Find Free WordPress Themes and plugins.

Text version of the video
csharp-video-tutorials.blogspot.com/2016/12/how-to-get-authenticated-user-identity.html

Slides
csharp-video-tutorials.blogspot.com/2016/12/how-to-get-authenticated-user-identity_13.html

All ASP .NET Web API Text Articles and Slides
csharp-video-tutorials.blogspot.com/2016/09/aspnet-web-api-tutorial-for-beginners.html

All ASP .NET Web API Videos

All Dot Net and SQL Server Tutorials in English
www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
www.youtube.com/c/KudvenkatArabic/playlists

In this video we will discuss how to get authenticated user identity name in asp.net web api and display it on the web page.

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

On the Login.html page, store the username in the browser local storage. The success() function is called when the user is successfully logged in. The response that we get from the server includes userName property which has the userName that is used to login. We are storing the logged in username in the broswer localstorage using the key userName.

success: function (response) {
localStorage.setItem(“accessToken”, response.access_token);
localStorage.setItem(“userName”, response.userName);
window.location.href = “Data.html”;
},

On the Data.html page include the following span element just below the “Load Employees” button. We will use this span element to display the logged in username.
[span id=”spanUsername” class=”text-muted”][/span]

Step 3 : On the Data.html page, in $(document).ready() function include the following line of jQuery code to retrieve and display the logged in username.
$(‘#spanUsername’).text(‘Hello ‘ + localStorage.getItem(‘userName’));

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

To see what the respective properties return, set a breakpoint in the Get() method of Employees controller and run the application in debug mode. Navigate to Login page and login. On the Data.html page click “Load Employees” button. When the execution pauses at the breakpoint, open Immediate Window and type the above properties one by one and press enter. To open immediate window, in Visual Studio select Debug – Windows – Immediate.

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.

Did you find apk for android? You can find new Free Android Games and apps.

Leave a Reply

16 Comments on "How to get authenticated user identity name in asp net web api"

Notify of
avatar

Subrat Kar
Guest
Subrat Kar
9 months 16 days ago

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

Haris Ahmed
Guest
Haris Ahmed
9 months 22 days ago

Please create tutorial authorization using roles thank you sir

rakesh kaddi
Guest
rakesh kaddi
10 months 5 days ago

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

Rashid Aslam
Guest
Rashid Aslam
10 months 17 days ago

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"

rakesh kaddi
Guest
rakesh kaddi
10 months 26 days ago

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

Neeraj Jadon
Guest
Neeraj Jadon
10 months 27 days ago

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.

achu r
Guest
achu r
11 months 3 days ago

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

Trần Công Sáng
Guest
Trần Công Sáng
11 months 7 days ago

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

RAMCHANDRA THAKKAR
Guest
RAMCHANDRA THAKKAR
11 months 7 days ago

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

mohammed alkhatari
Guest
mohammed alkhatari
11 months 7 days ago

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

wpDiscuz