Identity server 4 with asp.net identity registrati

2019-08-16 07:40发布

I want to know, how to correctly implement user registration in my identity server with asp net identity with redirection to login page after registration and then redirection to callback URL after login with registered account.

I followed Identity Server 4 quickstart tutorial and as far i created my own mvc identity server with asp.net identity. Now i want to add some registration so i created RegistrationController with Registration form and added Register button to login form.

I have an asp.net mvc application which require authentication. When user runs main page, he is automatically redirected to my identity server login page. User clicks register button, fills required information and clicks register button to confirm registration. Registration controller creates a new account and stores it in database using account manager.

This is part i am missing:

After successful registration i want an user to be redirected back to login page and when user logs in he should be redirected back to the web application and authenticated.

I am new to the web terminology especially mvc. Can you recommend me please some documentation where i can learn more to solve this problem ?

2条回答
劫难
2楼-- · 2019-08-16 07:57

I have faced similar problem in one of my projects and basically the way we achieved this desired behavior was to retain the original connect/authorize query parameters throughout the registration flow and then at the end redirect the user back to the connect/authorize url with the original query parameters.

All worked out of the box from that point on since it had the original callback uri to the client that initiated the OAuth flow in the first place.

查看更多
孤傲高冷的网名
3楼-- · 2019-08-16 08:04

Thanks Vidmantas Blazevicius tip i found solution. When user clicks register i redirect him to the register page with return URL as query parameter. When user confirms or cancels registration he is redirected back to AccountContoller.Login(string returnUrl) action. Then when user logs in, he is successfully redirected back to original site.

This is the AccountControler.Register action when user clicks register in login page:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Register(LoginInputModel model)
        => RedirectToAction("register", "registration", new { returnUrl = model.ReturnUrl });

This calls RegistrationController.Register to show registration form:

    [HttpGet]
    [Route("register")]
    public ViewResult Register([FromQuery]string returnUrl)
        => View("Views/Account/Registration.cshtml", new UserRegistrationViewModel(returnUrl));

The RegistrationController.Cancel action is executed when user clicks cancel in registration page:

    [HttpPost]
    [Route("cancel")]
    public IActionResult Cancel(UserRegistrationViewModel viewModel)
        => RedirectToAction("login", "account", new { returnUrl = viewModel.ReturnUrl });

In registration form use return URL property of view model like this @Html.HiddenFor(x => x.ReturnUrl) otherwise it will be not set in Cancel postback.

查看更多
登录 后发表回答