2017-11-13 4 views
0

Ich bin neu bei Identity Server. Ich habe es vorher nicht konfiguriert. Aber ich brauche es für ein Projekt, an dem ich arbeite. Die API wird einen Angular JS Client, eine iOS App und eine Android App bereitstellen. Wir müssen Authentifikation und Autorisierung implementieren und custmer gewährenIdentityServer 4 - invaid_client Fehler

Hinweis: Ich versuche, Identity Server und meine API in demselben Web-API-Projekt zu konfigurieren.

ich die Dokumentation und konfiguriert Identity Server wie folgt verfolgt haben:

In startup.cs, in 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(); 
      }); 
     }); 
    } 

I implementiert die Schnittstelle IExtensionGrantValidator und registrieren die Verlängerung Zuschuss

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; 
    } 
} 

Ich habe die Dokumentation gefolgt und die Client-Informationen wie folgt konfiguriert

 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()) } 
        } 
     }; 
    } 

Jetzt, da ich verwenden möchte es Angular JS, iOS und Android Ich möchte nur das Access Token erhalten vom IdentityServer, und dann die Access-Token verwenden für die Authentifizierung und Autorisierung.

für diesen Ich versuche, den/connect/Token von einem JS-Client

Aber ich bin immer einen invalid_client Fehler zuzugreifen.

@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

Die Serverantwort "error":"invalid_client"

log infos

Der Fehler, den ich auf der Server-Seite erhalten, ist 'No client identifier found':

1 - Warum bin ich diesen Fehler?

2 - Da ich den Token programmatisch in JS bekommen muss, muss ich/connect/token verwenden, bin ich richtig? Bin ich auf dem richtigen Weg?

Antwort

0

in NG2 Verwendung ein Verfahren wie unten:

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

Es ist Arbeit für mich, danke – Gerson

Verwandte Themen