ASP.net Core Identity Sign in as another user from Administrator

Hamza Khanzada

I am using ASP.net Core Identity, I have registered users in my application and I have assigned an Admin role to one of the users. Now I want that user to login to anyone else's account. As the Administrator of the site, that user should be able to sign in to any other user's account and can do any changes he wants.

Instead of creating a whole separate User management screen where admin will be able to update/delete users, I am thinking to let admin sign in to any of the existing user's account and change its information.

I am looking for a way in which my user which has an Admin role can sign in to any other user account in ASP.net Core Identity

David Liang

You can use the .SignInAsync() method from SignInManager to log in as a specific user without knowing their passwords:

public class ImpersonateUserHandler : IRequestHandler<ImpersonateUser, CommandResult>
{
    private readonly SignInManager<AppUser> _signInManager;

    public ImpersonateUserHandler(SignInManager<AppUser> signInManager)
    {
        _signInManager = signInManager;
    }

    public async Task<CommandResult> Handle(ImpersonateUser command, 
        CancellationToken cancellationToken)
    {
        try
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Look up the user you're going to impersonate
            var user = await _signInManager.UserManager.FindByIdAsync(command.UserId);

            ...

            // Login
            var signInResult = await _signInManager.SignInAsync(user, false);

            ...
        }
        catch(...)
        {
            ...
        }

        ...
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to sign out other user in ASP.NET Core Identity

ASP.net Identity 2.0 Sign-out another user

Application Insights User ID from ASP.NET Core identity

Why sign-in a new user when using ASP.NET Core Identity and token-based auth?

Why ASP.NET Core MVC 3.1 project wont let me sign in with a register user using Identity

How to get JWT authenticated sign-in user from identity claims in asp.net web api

ASP.NET Core Identity user groups

Google sign-in provider without ASP.NET Core Identity

ASP .NET Core - sign in as other user / impersonate user and way back

Access user identity from controller in ASP.NET Core Web API

Pass user detail after Login Is successful From identity asp.net core 3.1

How to retrieve Google profile picture from logged in user with ASP.Net Core Identity?

How to create new user accounts from another (admin role) account in ASP.NET MVC 5 (Identity)

How to link ASP.NET Core identity to another entity?

Refresh user cookie ticket in ASP.Net Core Identity

asp.net core 2.0 identity entity framework user not saved

InvalidOperationException when registering a new user with ASP .NET Core Identity and EntityFrameworkCore

Adding name to the user model in Asp.Net Core Identity

Store User Settings in ASP.NET Core Identity AspNetUsers Table or Not

ASP.NET Core Identity - get current user

Log IP of signed in user with ASP.net core Identity MVC

Mock User.Identity in ASP.NET Core for Unit Testing

ASP.NET core Identity seed only creates the last user

How to get Asp.net Core Identity User in View

ASP.NET Core Identity Role, Claim and User

ASP.NET Core 1.1 User Impersonation with Identity

ASP.NET Core Identity impersonate specific user

Add Collection to user object in ASP.NET Core with Identity

Modeling asp.net core identity user access