【问题标题】:Request access token with id_token, ADFS 2016 and react-adal使用 id_token、ADFS 2016 和 react-adal 请求访问令牌
【发布时间】:2018-12-14 13:57:15
【问题描述】:

我有一个客户端应用程序,我正在使用 ADFS 和 react-adal 进行身份验证,我已经完成了大部分工作,但似乎找不到请求访问令牌的方法(因此刷新令牌) 使用 id_token,这是我从 ADFS 获得的全部回报。

此时 React 应用程序将用户转发到 ADFS 进行登录,然后验证令牌,我希望能够从 /adfs/userinfo 获取用户信息(姓名、姓氏、角色等)端点,但需要不记名令牌才能这样做。

目前我的 componentWillMount 函数看起来像这样

componentWillMount = () => {
    axios.get('/home/AuthenticationContext').then((response) => {
        this.adalAuthentication = new AdalAuthentication(response.data);
        if (this.adalAuthentication.authenticateToken()) { // checks if authenticated with adfs, not app
            var error = this.adalAuthentication.Instance.getLoginError();
            if (error === "") {
                axios({
                    method: 'get',
                    url: '/identity/completeLogin/' + this.adalAuthentication.Instance.getCachedToken(this.adalAuthentication.Instance.config.clientId)
                }).then((response) => { console.log(response) })
                this.setState({
                    loggedIn: true
                });
            }
            else {
                this.setState({
                    error: error
                });
            }
        }
}

我遇到的问题是第二个 axios get 方法,它在同一个原点上触发了控制器操作。

    [HttpGet("completeLogin/{id_token}")]
    public async Task<IActionResult> CompleteLogin(string id_token)
    {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer" + id_token);
        var response = await client.GetAsync("https://adfs.domain/adfs/userinfo");

        return View();
    }

当然返回未经授权的响应。我找不到任何关于如何从 id_token 获取访问令牌的信息,或者在前端使用 Adal.js 库从 React 获取访问令牌的任何方式,有没有人通过这个?

编辑:我从 id_token 获得的唯一信息如下。

"userName":"bob@email.com",
"profile": {
    "aud":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "auth_time":1544777683,
    "exp":1544793006,
    "iat":1544789406,
    "iss":"https://adfs.domain/adfs",
    "mfa_auth_time":1544777705,
    "nonce":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "sid":"x-x-x-xx-xxxxxxxxxx-xxxxxxxx-xxxxxxxxxx-xxxx",
    "sub":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "unique_name":"T1234\\bob",
    "upn":"bob@email.com"
}

【问题讨论】:

  • 您似乎在做一些倒退的事情。我会放置一些身份验证中间件并保护该控制器作为第一个调用点。如果需要,您可以重放令牌以获取用户信息,但这通常是客户端任务。我还可以看到您的代码存在一个直接问题,即 Bearer 和令牌之间没有空格。就这么简单。

标签: reactjs adal.js adfs4.0


【解决方案1】:

这似乎是 adal.js 库的普遍问题。

here 描述的解决方法可能会有所帮助。

本质上,将 GET 更改为 POST。

【讨论】:

  • 感谢您的建议,我已经尝试过了,但仍然不满意。我在 adfs adfs/oauth2/token 上找到了另一个端点,但仍然无法取回访问代码。我在想我可能不得不切换到 oidc
  • 是的,我已经看到了这一点,它只是获取了用户信息和/或一个不起作用的访问令牌,尽管有什么,adal 似乎无法取回文档说,只是 id 令牌
猜你喜欢
  • 2020-06-20
  • 2017-07-01
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
相关资源
最近更新 更多