How can I redirect to the same window after email confirmation?

Stanislav Panferov 50 Reputation points
2023-05-19T14:39:10.5066667+00:00

I'm trying to implement two factor authentication by sending a confirmation email with a link

<a class="confirm-button" href="@url">Confirm</a>

...

string body = GetConfirmationMailBody();
body = body.replace("@url", $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/UserAccount/CompleteConfirmEmail/?{ModelToQueryString(model)}");

to a controller action that validates a code for the user with a custom token provider and should redirect to the "Login" action if the code is correct. And I need to open the corresponding "Login" view in the same window as the current "Register" view. But confirmation mail link always open new browser window.

[HttpGet]
public async Task<IActionResult> CompleteConfirmEmail(RegisterViewModel model)
{
    IActionResult actionResult = RedirectToAction("Register", model);
	if(!string.IsNullOrWhiteSpace(model?.EmailVerificationCode))
	{
		BankApiUser user = await UserManager.FindByEmailAsync(model?.Email);
		if(user != null && await UserManager.VerifyTwoFactorTokenAsync(user, Startup.Startup.TwoFactorTokenProviderName, model.EmailVerificationCode))
		{
			model.EmailConfirmationState = ConfirmationState.Complete;
			model.Submitted = false;
            actionResult = RedirectToAction("Login", new LoginViewModel(model));
        }
	}
    return actionResult;
}
[HttpGet]
public IActionResult Login(LoginViewModel? model = null)
{
    return View(model ?? new LoginViewModel());
}

I tried to set name to my current window in javascript.

window.name = "BankApi.Register";

And then open it by name.

[HttpGet]
public async Task<IActionResult> CompleteConfirmEmail(RegisterViewModel model)
{
    string action = "Register";
    if(!string.IsNullOrWhiteSpace(model?.EmailVerificationCode))
    {
        BankApiUser user = await UserManager.FindByEmailAsync(model?.Email);
        if(user != null && await UserManager.VerifyTwoFactorTokenAsync(user, Startup.Startup.TwoFactorTokenProviderName, model.EmailVerificationCode))
        {
            model.EmailConfirmationState = ConfirmationState.Complete;
            model.Submitted = false;
            action = "Login";
        }
    }
    if(model == null)
    {
        model = new RegisterViewModel();
    }
    string url = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}/UserAccount/{action}/?{ModelToQueryString(model)}";
    string target = "BankApi.Register";
    return Content(@$"
        <script>
            try
            {{
                window.close();
                window.open({url}, {target});
            }}
            catch(e)
            {{
                console.error(e);
                throw e;
            }}
        </script>",
        "text/html");
}

But it is not working. How can I open redirect link in the current window?

You can find more details in my solution repo: BankAccountingPublic

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-05-19T14:57:03.5866667+00:00

    You can’t. That is why it’s common to display a code in the email that the user enters in the registration form, rather than click a link.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.