Identity Server 4 Auto Login After Registration

陌路散爱 提交于 2020-01-01 17:11:46

问题


I want the user to be auto logged after registration to the MVC app (email verification is not needed now), I have followed this sample for IdSrv3 and edited some parts:

http://benfoster.io/blog/identity-server-post-registration-sign-in

Here is my Setup:

MVC:

public class AuthController : Controller
{
    [HttpGet]
    [AllowAnonymous]
    public async Task LogIn()
    {
        var properties = new AuthenticationProperties
        {
            RedirectUri = Url.Action("index", "home", HttpContext.Request.GetUri().Scheme)
        };

       await HttpContext.Authentication.ChallengeAsync(properties);
    }

    [HttpGet]
    [AllowAnonymous]
    public IActionResult Register()
    {
        var returnUrl = $"http://localhost:5002/auth/login";
        return Redirect($"http://localhost:5000/Account/Register?returnUrl={returnUrl}");
    }
}

Identity Server 4 using ASp.Net Core Identity

[Authorize]
    public class AccountController : Controller
    {
       //
        // GET: /Account/Register
        [HttpGet]
        [AllowAnonymous]
        public IActionResult Register(string returnUrl = null)
        {
           ViewData["ReturnUrl"] = returnUrl;
           return View();
        }

    //
    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var otac = user.GenerateOTAC(TimeSpan.FromMinutes(1));
                await _userManager.UpdateAsync(user);
                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation(3, "User created a new account with password.");
                if (string.IsNullOrEmpty(returnUrl))
                {
                    return RedirectToAction(nameof(HomeController.Index), "Home");
                }
                else
                {
                    return Redirect(returnUrl);
                }
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }
}

I could not implement the OTAC handshake as the sample shows becaus there is no User Service any more in IdSrv 4.

The scenario works as follows:

1 - User clicks 'Register' in MVC app whish redirects to the IdSrv Account->Register passing the MVC Auth->login as a return URI. 2 - After the user completed the registration in IdSrv another redirect executes to return back to the MVC Auth->Login. 3 - The MVC Auth->Login creates a challenge and since the user is already signed in in the registration process using the cookie, the authentication cookie gets merged (as per the IdSrv log), and there is no login screen appears and now the user is logged in and landed to the MVC app.

Does this setup have any secutity flaw?, is there any way I can implement the OTAC handshake in IdSrv4?

thanks,


回答1:


In your case maybe this could work

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;
    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email 
        var result = await _userManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            await _signInManager.SignInAsync(user, isPersistent: false);
            return LocalRedirect(returnUrl);
        }
        AddErrors(result);
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}


来源:https://stackoverflow.com/questions/43040553/identity-server-4-auto-login-after-registration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!