Use Identity with existing database

Asked

Viewed 54 times

0

I am migrating from a system to Asp core, but the system currently has its login logic made in the previous one, my question is the following, is it possible to use only the authorization of Identity (Roles)? (I searched in several articles did not find making with existing bank).

  • You want to log in to the "hand" and the system control the user with the roles?

1 answer

0

You can "force" authentication by searching the logged in user with the rule you want, I particularly developed a mobile app and put/created a default user to log in every time the app tries to access my page and then "force" authentication and after authentication I redirect to desired page. In my case it is already registered default user, but you can do the search by Scroll or pass the Scroll and if everything is all right you do the command below. the name parameter is the user of my app I pass, I do not check the password why it is internal distribution app. The Signinmanager class that is responsible for user authentication. I hope I helped

Authentication is in command: await _signInManager.Signinasync(user, true);

My controller to authenticate my app.

public class AppLoginController : Controller
{
    private readonly ApplicationDbContext _context;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;


    public AppLoginController(ApplicationDbContext context, UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
    {
        _context = context;
        _userManager = userManager;
        _signInManager = signInManager;

    }

    [HttpGet]
    public async Task<IActionResult> Index(string name)
    {

        var user = await _userManager.FindByNameAsync(name);

        if (user != null)
        {

           await _signInManager.SignInAsync(user, true);

            return RedirectToAction("Index", "MeuController");

        }

        return RedirectToAction("Index", "Home");


    }
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.