c# - how can I access an `Microsoft.AspNetCore.Authorization` authorised user's details

jamheadart

I've set up authorisation on my site, which uses context_A - now all I need to do is add the [Authorize] attribute to the top of any controller to make sure the user is signed in to use it.

I've done this on one particular controller which uses another context, context_B - the authorisation still works, but now I'm left wondering how I can access the authorised user's details on the controller that uses a different context.

using Microsoft.AspNetCore.Authorization;

namespace projectA.Controllers
{
    [Authorize] // this is authorised using contextA
    public class SomeController : Controller
    {
        private readonly ContextB _context;

        public SomeController(ContextB context) // I pass in completely different context here
        {
            _context = context;
        }
    }
}

I want to access the auth context in my controller, something like

`string x = AuthorisationContext.CurrentUser.Email`

but I don't know what's going on behind the scenes when using [Authorize] - I'm not sure where/when it's pushing and pulling data using contextA

I'm guessing I probably should have the authorisation done within the same context for loads of reasons, but is it possible to pluck the current authorised user's details into the controller even though I've simply added the [Authorize] attribute?

Xueli Chen

From the Controller base class, you can get the IClaimsPrincipal from the User property. Other fields can be fetched from the database's User entity:

1.Get the user manager using dependency injection

private UserManager<IdentityUser> _userManager;

private readonly ContextB _context;

public SomeController(ContextB context, UserManager<IdentityUser> userManager)
{
        _context = context;
        _userManager = userManager;
}

2.And use it:

public async void GetUserInfo()
{
        var user = await _userManager.GetUserAsync(User);
        var email = user.Email;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I access another user's resources (specifically emails) using Microsoft Graph in an MVC app?

How can I setup SwashBuckle.AspNetCore.Swagger to use Authorization?

How to identify the authorised user based on Google calendar's callback

Authorization Code Grant: how can I get an access token when the user is not present?

How can i set scope authorization for a route in fastify and check user has access to that route or not with Node.js

How do I create a "Details" button in Microsoft Access?

How to check the presence of Microsoft.AspNetCore.Authorization attributes in an IEnumerable<CustomAttributeData>?

How can I set the Firebase Firestore rules if I want the user can access even there's no current user

How can I find the manager's details for each user who is an ADGroupMember

Redirecting after login to authorised user's relationship

Spotify authorization code (not access token) is expiring - how can I circumvent this?

What are the common pitfalls that would stop Authorised Key SSH access, and how do I find and correct for them?

How can I access Google+ user's email and pictures address through OWIN in ASP.NET 4.5 C#?

How can other users view a user's profile details in octobercms?

How can I verify token and get user details in keycloak?

How can i allow a user to edit their own account details?

When the user logs in how can I get that particular users details?

How can I update the details of current user using forms and views

How can I crop the area containing the recipient's details in a mail through Image Processing in C#?

How can I access field value by it's offset in C#

C# Why can't I access domain user's local folder under C:\Users?

How can I manually get an access token using OAuth 2.0 authorization code flow in Azure Active Directory B2C?

How to access Microsoft Graph's services with service-user credentials

How can I insert into a table both a select max function from another table and variables input by the user, using PHP, SQL and Microsoft Access?

How can I get Record's details using Rtti?

How can I keep user keep user logged in RingCentral while using Authorization code flow?

How can I access a user's music library without using MusicKit?

How can I access user's data from an identity credential provided by a Cognito identity pool?

How can I see who has access to a user's calendar in office 365?