2017-11-13 2 views
0

Identity Serverを初めて使用しています。私はこれまでに設定していません。しかし、私が取り組んでいるプロジェクトにはそれが必要です。 APIはAngular JS Client、iOS App、Android Appを提供します。IdentityServer 4 - invaid_clientエラー

注:同じWeb APIプロジェクトでIdentity ServerとそのAPIを設定しようとしています。

私は次のように文書化され、構成のIdentity Server続いている

:私はインターフェイスIExtensionGrantValidatorを実装し、拡張助成金を登録ConfigureServices()

private readonly IConfiguration config; 

    private const string DEFAULT_CORS_POLICY = "localhost"; 

    public Startup (IConfiguration config) => this.config = config; 

    public void ConfigureServices (IServiceCollection services) { 
     services.AddIdentityServer() 
      .AddDeveloperSigningCredential() 
      //.AddInMemoryApiResources(config.GetSection("ApiResources")) 
      .AddInMemoryApiResources (Config.GetApis()) 
      //.AddInMemoryClients(config.GetSection("Clients")) 
      .AddInMemoryClients (Config.GetClients()) 
      .AddInMemoryIdentityResources (Config.GetIdentityResources()) 
      //.AddInMemoryIdentityResources(config.GetSection("IdentityResources")) 
      .AddExtensionGrantValidator<WechatGrantValidator>(); 

     services.AddTransient<IUserCodeValidator, UserCodeValidator>(); 

     services.AddCors (options => { 
      options.AddPolicy (DEFAULT_CORS_POLICY, builder => { 
       builder.WithOrigins ("http://localhost:5202"); 
       builder.AllowAnyHeader(); 
       builder.AllowAnyMethod(); 
      }); 
     }); 
    } 

にstartup.csで

を、

public class WechatGrantValidator : IExtensionGrantValidator { 

    private IUserCodeValidator validator; 

    public WechatGrantValidator (IUserCodeValidator validator) { 
     this.validator = validator; 
    } 
    public string GrantType => "wechat_grant"; 

    public async Task ValidateAsync (ExtensionGrantValidationContext context) { 
     string userCode = context.Request.Raw.Get ("userCode"); 
     var result = await validator.ValidateAsync (userCode); 
     if (result.IsError) { 
      context.Result = new GrantValidationResult (TokenRequestErrors.InvalidGrant); 
      return; 
     } 
     context.Result = new GrantValidationResult (result.UserId, GrantType); 
     return; 
    } 
} 

私は以下のようにドキュメントと設定されたクライアント情報を守っています

 public static IEnumerable<Client> GetClients() { 
     return new Client[] { 
        new Client { 
        ClientId = "javascritpClient", 
        ClientName = "JavaScript Client", 
        AllowedGrantTypes = { "wechat_grant" }, 
        AllowAccessTokensViaBrowser = true, 
        AllowedCorsOrigins = { "http://localhost:5202" }, 
        AllowedScopes = { "api1" }, 
        ClientSecrets = { new Secret ("secret".Sha256()) } 
        } 
     }; 
    } 

は、今、私が使用したいので、それアンギュラJS、私はちょうどIdentityServerからアクセストークンを取得し、その後、認証と認可のためのアクセストークンを使用したいのiOSとAndroid。このため

私はJSクライアントから

を/接続/トークンにアクセスしようとしています。しかし、私はinvalid_clientエラーを取得しています。

@Injectable() 
export class OauthService { 
    private http: Http; 
    public constructor(http: Http) { 
    this.http = http; 
    } 

    public async getDiscoveryInfos(issuer: string): Promise<DiscoveryInfos> { 
    if (!issuer.endsWith('/')) { 
     issuer += '/'; 
    } 
    issuer += '.well-known/openid-configuration'; 
    return this.http.get(issuer).map(response => { 
     return response.json(); 
    }).toPromise(); 
    } 

    public async getToken(): Promise<any> { 
    const headers = new Headers({ "Content-Type": "application/x-www-form-urlencoded" }); 
    const discovery = await this.getDiscoveryInfos('http://localhost:5200'); 
    return this.http.post(discovery.token_endpoint, { 
     grant_type: 'wechat_grant', 
     userCode: 'userCodeAA', 
     client_id: 'javascritpClient', 
     client_secret: 'secret', 
     scope:'api1' 
    }, { headers: headers }).map(response => response.json()).toPromise(); 
    } 

} 

http response infos

サーバの応答"error":"invalid_client"

log infos

私は、サーバー側で取得エラーが'No client identifier found'です:

1 - なぜ私はこのエラーを取得していますか?

2 - トークンをJSでプログラムで取得する必要があるため、/ connect/tokenを使用する必要がありますが、これを修正しますか?私は正しい道にいるのですか? NG2で

答えて

0

は怒鳴るようなメソッドを使用します。それは私のための仕事だ

public Token(data: SigninModel): Observable<any> { 
    this.options = new RequestOptions({ headers: this.headers }); 
    this.headers.append('Content-Type', 'application/x-www-form-urlencoded'); 
    const url = this.urlBase + `connect/token`; 
    const param = new URLSearchParams(); 
    param.set('grant_type', 'password'); 
    param.set('client_Id', 'javascritpClient'); 
    param.set('client_secret', 'secret'); 
    param.set('scope', 'offline_access'); 
    param.set('username', data.username); 
    param.set('password', data.password); 
    return this.http.post(url, `${param.toString()}`, this.options) 
     .map((response: Response) => { 
      return (response.json()); 
     }) 
     .catch(this.handleError); 
} 
+0

を、ありがとう – Gerson

関連する問題