首页 > 解决方案 > 带有 SPA 和 hellojs 的 IdentityServer4

问题描述

我基于 dotnet 新模板“IdentityServer4 快速入门 UI(仅限 UI 资产)”(is4ui)创建了一个 IdentityServer。

这与服务器渲染的 MVC 应用程序一起工作得很好,但我无法让它与使用 hellojs 的 SPA 一起工作。

我正在摆弄 url 以从 Identity Server 显示登录页面。登录逻辑运行良好,但我认为缺少任何小块来正确重定向到带有访问令牌的客户端应用程序。

这是我用来初始化 hellojs 的代码:

const AuthorityUrl = 'http://localhost:5000';
hello.init({
  'openidconnectdemo': {
    oauth: {
      version: '2',
      auth: AuthorityUrl + '/Account/Login',
      grant: AuthorityUrl + '/Grant'
    },
    scope_delim: ' '
  }
});

hello.init({
  openidconnectdemo: this.authConfig.clientId
});

这是登录代码

hello.login('openidconnectdemo', {
  redirect_uri: this.authConfig.redirectUrl,
  scope: `openid profile`,
  response_type: 'token',
  display: 'page',
});

在 AccountController 中的这段代码中,Identity Server 尝试重定向到应用程序,但由于 ReturnUrl 为空而失败:

                if (Url.IsLocalUrl(model.ReturnUrl))
                {
                    return Redirect(model.ReturnUrl);
                }
                else if (string.IsNullOrEmpty(model.ReturnUrl))
                {
                    return Redirect("~/");
                }
                else
                {
                    // user might have clicked on a malicious link - should be logged
                    throw new Exception("invalid return URL");
                }

我是否必须修改身份服务器才能使它们一起工作,或者客户端部分是否缺少某些东西?

感谢您的任何建议!

标签: single-page-applicationidentityserver4openid-connecthello.js

解决方案


我可能会推荐使用 oidc-client-js,因为它是 OIDC 的完整实现,应该更容易开始工作。

也就是说,看起来您的第一个问题是 auth URL 应该指向授权端点而不是 UI 中的登录 - 即它应该是/connect/authorize而不是/account/login.


推荐阅读