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.
How can I redirect to the same window after email confirmation?

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