Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

 

The following code shows how to determine your own UserID as a GUID from an Asp web server in a SPA client application to evaluate data.

 

Angular, Typescript, Asp Core . Net5 Web API, Microsoft ASP Server Identity

 

Query UserID from Identity Server from Angular App, SPA Frontend in Asp Core . Net5

 

 

  load_userID() {

    //--------< load_userID() >--------

    //*load own userid as guid and check if the article is own, owner

    this.dataService.get_UserIdGuid().subscribe(response => {

      //console.log("get_UserIdGuid()=" + response);

      //console.log("article=" + JSON.stringify(this.article));

 

      this.iduserguid = response;

      if(this.article.iduserguid==this.iduserguid){

        this.isOwner=true;

      }

      else  {

        this.isOwner=false;

      }

    }

    );

    //--------</ load_userID() >--------

  }

 

 

 

 

API Request

In the Angular component service (here article.service.ts)

Then http sends an API request to the ASP Core API server.

This returns the MS Identity User as a GUID in Json format. Here in Angular/Typescript Observable and httpClient

useridguid: "1428ca0b-186c-43c8-8ec6-08d26e97e83b"

  get_UserIdGuid(): Observable<string> {

    //----< get_UserIdGuid >----

    //useridguid: "1428ca0b-186c-43c8-8ec6-08d26e97e83b"

    return this.http

      .get<{useridguid}>(this.url_Api_Base + "get_UserIdGuid" )

      .pipe(

        map(({useridguid})=>{

          return useridguid

        }

        )

      )

      //----</ get_UserIdGuid >----

  }

 

 

 

An API method is stored in the Asp Core Server, which can return a logged-in user as a GUID

 

        // GET: api/getuserid/5 ============👁

        // EventsController

        [Authorize]

        [Route("get_UserIdGuid")]

        [HttpGet("get_UserIdGuid")]

        public async Task<ActionResult<string>> get_UserIdGuid()

        {

            //-------------< Liste: GetArticles >-------------

           

            //--< Get User ID >--

            //internal referenz-Number for tracking in tables

            String sIDUserGuid = User.getUserIdGuid();   //*ID in aspUsers wie: 1428ca0b-186c..

            if (sIDUserGuid == "") return BadRequest();

            //--</ Get User ID >--

 

            return new JsonResult(new { useridguid= sIDUserGuid });

            //-------------</ Liste: GetArticles >-------------

        }

 

 

To do this, you have to enter a static method on the ASP server, which can read out the Identity User Claims

 

//< Using >

using System.Security.Claims;

//</ Using >

 

 

public static class UserInfo_Methods

{

    //-------------< Class: ExtensionMethods >-------------

    public static string getUserIdGuid(this ClaimsPrincipal user)

    {

        //------------< getUserId(User) >------------

        //*returns the current UserID

        if (!user.Identity.IsAuthenticated)

            return null;

 

        ClaimsPrincipal currentUser = user;

 

        //< output >

        return currentUser.FindFirst(ClaimTypes.NameIdentifier).Value;

        //</ output >

        //------------</ getUserId(User) >------------

    }

Mobile

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de