Identity server 4 with asp.net identity registration

亡梦爱人 提交于 2019-12-14 03:11:31

问题


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 ?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/56011116/identity-server-4-with-asp-net-identity-registration

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