Get the user name, using the Excel Macro that is calling an API (C#) , in the API code

40 Views Asked by At

I am working to make a solution like the following. An Excel Macro is calling an API written in C#. The C# API takes the user name and does some validation by checking the DB. Currently the UserName is passed like the following

UserName = Environ$("UserName") 
RequestMessage & "<UserName>" & UserName & "</UserName>"

However, as you would agree anybody can manipulate the UserName variable. I would like to check the User name in the API, by other means. In the environment we have AD authentication enabled. What should be the possible ways to achieve this. I am not really very highly skilled with API using C#.

I tried this code

string _userName = System.Web.HttpContext.Current.User.Identity.Name;

But that is returning error "HttpContext" does not exist.

I am using .Net Core 8.0.

Any guidance will be helpful.

1

There are 1 best solutions below

0
Sabyasachi Mukherjee On BEST ANSWER

I have used the following code to achieve what I wanted.

WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
 _httpContextUserName = wp.Identity.Name

This code above is giving me the user name, who is calling the API.