This article explains an easy approach on how to get the Current User in ASP.NET Core.
You may find a need to get a logged-in username using your application in the IIS or Cloud-hosted app.
One can easily get the current user name from
HttpContext
in ASP.NET Core provided user details are transferred for the given session using cookies or other means. etc.
Today in this article, we will cover below aspects,
If debugging locally to verify, Please update the launchSettings.json file for the Windows authentication setting as true in the ‘iisSettings’ section as below.
windowsAuthentication”: true,
“anonymousAuthentication”: false
HttpContext.User.Identity.Name is empty
If the above-discussed setting is not followed then you will get HttpContext.User.Identity.Name as empty or null.
Cloud-hosted app Windows authentication will also work if enabled with Windows Authentication using the Active Directory (AD group) or by following other security policy with in the enterprise infrastructure which let you sync up your User IDs with Server/Cloud.
Most enterprise Application uses secured cookies and session token details (with the help of redirection from their intranet sites to the cloud hosted web applicaiton) to send the actual user name logged in to an application.
Please bookmark this page and share it with your friends. Please Subscribeto the blog to receive notifications on freshly published best practices and guidelines for software design and development.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.