在SPA中对用户进行身份验证的位置?

时间:2016-06-29 06:37:59

标签: asp.net authentication aurelia auth0

我有一个连接到ASP.NET Core后端的Aurelia SPA。我使用Auth0进行身份验证(使用aurelia-auth,而不是Auth0 Lock小部件)。

我目前直接使用Auth0登录,而不是使用SPA。这给了我一些问题,因为我的API中的Auth0实现需要id_token而不是access_token。可以通过告诉aurelia-auth将id_token用作访问令牌来传递此问题。但这使Auth0和Aurelia应用程序之间的进一步通信变得复杂化。 Auth0期望access_token用于用户配置文件调用等。

我应该通过自己的API进行身份验证吗?或者我应该在Aurelia中制作两个不同的fetch-clients?一个用于调用我的API(使用id_token),另一个用于调用Auth0 API(使用access_token)。

1 个答案:

答案 0 :(得分:5)

我已经写了很多关于这个主题的博客,我将在下面链接它们以供进一步阅读。我的建议是创建一个单独的"身份验证" root viewModel,可供所有用户使用,与您的" app" root viewModel,仅供登录用户使用。

<强> main.js

import AuthService from 'AuthService';

export function configure(aurelia) {
    aurelia.use
    .standardConfiguration()
    .developmentLogging();

  // After starting the aurelia, we can request the AuthService directly
  // from the DI container on the aurelia object. We can then set the 
  // correct root by querying the AuthService's isAuthenticated method.
  aurelia.start().then(() => {
        var auth = aurelia.container.get(AuthService);
        let root = auth.isAuthenticated() ? 'app' : 'login';
        aurelia.setRoot(root);
    });
}

进一步阅读