身份服务器注册不会重定向回React应用 [英] Identity server registration doesn't redirect back to React app

查看:89
本文介绍了身份服务器注册不会重定向回React应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core后端,其中的React前端托管在不同的来源.

I have an ASP.NET Core backend with a React frontend hosted in different origins.

ASP.NET核心后端配置为使用内置身份服务器:

The ASP.NET core backend is configured to use the inbuilt identity server:

// Startup
public void ConfigureServices(IServiceCollection services)
{
  ...
  services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
  ...
}

我已经添加了身份服务器期望的OidcConfigurationController:

I have added the OidcConfigurationController that the identity server expects:

public class OidcConfigurationController : Controller
{
    public OidcConfigurationController(IClientRequestParametersProvider clientRequestParametersProvider)
    {
        ClientRequestParametersProvider = clientRequestParametersProvider;
    }

    public IClientRequestParametersProvider ClientRequestParametersProvider { get; }

    [HttpGet("_configuration/{clientId}")]
    public IActionResult GetClientRequestParameters([FromRoute]string clientId)
    {
        var parameters = ClientRequestParametersProvider.GetClientParameters(HttpContext, clientId);
        return Ok(parameters);
    }
}

我还在身份服务器读取的appsettings.json中添加了以下设置:

I have also added the following settings in appsettings.json that the identity server reads:

...
"IdentityServer": {
  "Clients": {
    "WebApplication1": {
      "Profile": "SPA",
      "RedirectUri": "http://localhost:3000/authentication/login-callback",
      "LogoutUri": "http://localhost:3000/authentication/logout-callback"
    }
  }
},
...

React应用程序托管在http://localhost:3000上,并使用oidc-client与ASP.NET Core服务器进行交互.前端代码似乎可以正确请求登录,并传递正确的返回网址:

The React app is hosted at http://localhost:3000 and uses oidc-client to interact with the ASP.NET Core server. The frontend code appears to correctly request a sign in passing the correct return url:

ASP.NET Core身份验证页面成功显示:

The ASP.NET Core authentication pages are successfully shown:

但是,如果发布新注册,则ASP.NET Core重定向到其根目录,而不是http://localhost:3000:

But if you post a new registration, ASP.NET Core redirects to its root rather than http://localhost:3000:

有什么我想念的东西吗?还是内置的ASP.NET身份仅在客户端以相同的来源托管时才起作用?

Is there anything I've missed or does the inbuilt ASP.NET identity only work if the client is hosted in the same origin?

任何帮助表示赞赏.

推荐答案

在往返Account/Register的过程中,您只是错过了返回URL.这与起源无关.使用纯登录功能进行检查-应该可以立即使用.

You just miss your return url during roundtrip to Account/Register. That has nothing to do with origins. Check with a pure signin -- that should work out of the box.

新帐户注册不是Identityserver负责的.您必须自己处理.每次重定向时,您都需要通过返回URL,从登录表单上的注册"按钮开始,到您的[HttpPost]Register操作结束.即使用户取消中间的注册并决定使用现有帐户登录,您也很可能希望保留该URL.

New account registration is not what Identityserver is responsible for. You have to handle that yourself. You need to pass through your return url each time you redirect, starting from the "Register" button on your login form and ending at your [HttpPost]Register action. Most likely you would like to keep that url even when you user cancels the registration in the middle and decides to signin with an existing account.

请参阅问题/答案作为参考.

See this question/answer for the reference.

这篇关于身份服务器注册不会重定向回React应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆