使用与Identity Server 4进行提取

时间:2017-12-01 14:21:00

标签: javascript node.js identityserver4 fetch-api isomorphic-fetch-api

使用资源所有者密码流时,如何使用IdentityServer4获取获取JWT令牌? (大部分时间都不推荐)。

即使我应该使用隐式流程,我也想使用此流程,因为在我的spa应用程序中,它更方便。 首先,您可以在此处找到有用的信息来实现您的解决方案:

但是如何使用[fetch-api]从IdentityServer 4获取令牌?

    const form = new FormData();
    form.append('grant_type', 'password');
    form.append('username', username);
    form.append('password', password);
    if (this._settings.scope) {
        form.append('scope', this._settings.scope);
    }
    form.append('client_id', this._settings.clientId);
    if (this._settings.clientSecret) {
        form.append('client_secret', this._settings.clientSecret);
    }

    var options = {
        method: 'POST',
        headers: {
           'Content-Type': 'multipart/form-data'
        },
        rejectUnauthorized: false, // when use local unverified certificate
        body: form
    };

我应该收到一个JWT令牌作为响应,但我收到一个http 500错误。 "内部服务器错误"

如果我使用fiddler跟踪HTTP标头,我得到以下结果:

POST http://127.0.0.1:8888/ HTTP/1.1
content-type: multipart/form-data
accept-encoding: gzip,deflate
user-agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch)
connection: close
accept: */*
content-length: 896
Host: 127.0.0.1:8888

1 个答案:

答案 0 :(得分:0)

正如我在之前的评论中提到的:如果您想使用Fetch来发布带有表单数据的授权请求,则不得在标题中指定内容类型。由于fetch会自动使用正确的值填充它(" multipart / form-data")并添加动态生成的内容边界。 另请注意,如果您更喜欢使用application / x-www-form-urlencoded内容类型方法,则可以使用URLSearchParams