代码之家  ›  专栏  ›  技术社区  ›  Armand

与Identity Server 4一起使用fetch

  •  0
  • Armand  · 技术社区  · 7 年前

    当使用资源所有者密码流时,如何使用带有IdentityServer4的fetch来获取JWT令牌?(大多数情况下不建议这样做)。

    首先,您可以在这里找到有用的信息来实施您的解决方案:

    但是如何使用[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 回复  |  直到 7 年前
        1
  •  0
  •   Armand    7 年前

    正如我在之前的评论中提到的:如果你想使用 Fetch 为了使用表单数据发布授权请求,您不得在标题中指定内容类型。自从 fetch 将自动使用正确的值(“多部分/表单数据”)填充它,并添加动态生成的内容边界。 另请注意,您可以使用 URLSearchParams ,如果您喜欢使用应用程序/x-www-form-urlencoded content-type方法。