HDTutorial
EN

Playback starts YouTube’s embedded player (privacy-enhanced mode).

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

132,253 views October 31, 2017 8:01

Watch on YouTube
Channelkudvenkat

Description from the creator

Text version of the video
csharp-video-tutorials.blogspot.com/2016/12/how…

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.
youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?s…

Slides
csharp-video-tutorials.blogspot.com/2016/12/how…

All ASP .NET Web API Text Articles and Slides
csharp-video-tutorials.blogspot.com/2016/09/asp…

All ASP .NET Web API Videos
youtube.com/playlist?list=PL6n9fhu94yhW7yoUOGNO…

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

All Dot Net and SQL Server Tutorials in Arabic
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.

About this tutorial

kudvenkat published this 8:01 video tutorial on October 31, 2017. It has been watched 132,253 times on YouTube.

Channel
kudvenkat
Duration
8:01
Published
October 31, 2017
Views
132,253
Likes
411

Tags

Share

This tutorial in other languages