Get current sharepoint online logged in user from C#

Copper Contributor

Hi, 

 

We are creating a portal site using sharepoint online.  In one of the web part we are embedding our internal app which was created in C# code. I need to get the current logged in sharepoint user in this c# code to do the data filter based on users. Have tried sharepoint REST API but it gives the user of the registered app. Appreciate if any idea?

 

Thanks. 

4 Replies
Hi Kannan,
SharePoint Online doesn't allow custom C# web parts. You might have SharePoint Framework web parts or you might have provided hosted Add-in. Put some code sample here to know what exactly you trying to achieve and would able to comment.
So presumably you're talking about a SharePoint provider hosted add-in, in which case, you can get your Client Context object (which will be different depending on how you authenticate), then access the Web property, which has a "Current User". This article shows you how to get a Client Context in your provider hosted add-in and access the Web property - from which you can then access the "Web.CurrentUser" property https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/get-started-creating-provider-hosted-shar....

Thank you @Rahul Suryawanshi  and Thomas Berman for your time. I have fixed this by creating new SPFx web part that can show an iframe and pass the username as a parameter to the embedded page. This solve the issue. 

@kannan_mck007 

I have the same issue, can you explain how you created "new SPFx web part". I'm really new to SharePoint so excuse my ignorance.