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

Stanislav Panferov 45 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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,391 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,414 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,633 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,416 Reputation points
    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.