2016-01-13 3 views
5

Ich baue eine API mit asp.net 5 RC2. Ich versuche, openiddict-core, gefunden here für lokale Konten zu implementieren, und ich möchte auch zulassen, dass Benutzer externe Logins wie Google verwenden.Kein Authentifizierungshandler ist für die Authentifizierung für das Schema konfiguriert: Microsoft.AspNet.Identity.External

Ich habe alles eingerichtet, aber wenn ich versuche, die Google-Authentifizierung zu implementieren, und ich nenne diesen Code

var info = await _signInManager.GetExternalLoginInfoAsync();

erhalte ich die Fehlermeldung im Titel.

Auf dem Client bin ich mit Satellizer gefunden here, die kümmert sich um das Öffnen der Google Eingabeaufforderungsfenster und senden Sie den Rückruf an meine AuthController Google-Methode, die den normalen ChallengeResult-Code in anderen MVC6 Beispiele sehen.

I-Code haben, schrieb die Benutzer erhalten Details manuell und das funktioniert, aber ich dachte, ich die bereits gebaut signInManager stattdessen verwenden würde, anstatt das Rad reproduzieren ...

ich die Dinge gesetzt werden korrekt nicht auf , da alle Beispiele anscheinend Cookies verwenden, was vermutlich darauf zurückzuführen ist, dass es sich um mvc6-Webanwendungen handelt, nicht um eine API. Ich möchte keine Cookies verwenden, aber das könnte mein Problem sein.

Jetzt für etwas Code.

startup.cs

public void ConfigureServices(IServiceCollection services) 
{ 
    // Add MVC services to the services container. 
    services.AddMvc(); 

    services.AddEntityFramework() 
     .AddSqlServer() 
     .AddDbContext<ApplicationDbContext>(options => 
     options.UseSqlServer(_configuration["Data:DefaultConnection:ConnectionString"])); 

    services.AddIdentity<ApplicationUser, IdentityRole>() 
     .AddEntityFrameworkStores<ApplicationDbContext>() 
     .AddDefaultTokenProviders() 
     .AddOpenIddict(); // Add the OpenIddict services after registering the Identity services. 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    // use jwt bearer authentication 
    app.UseJwtBearerAuthentication(options => 
    { 
     options.AutomaticAuthenticate = true; 
     options.AutomaticChallenge = true; 
     options.RequireHttpsMetadata = false; 
     options.Audience = "http://localhost:5000/"; 
     options.Authority = "http://localhost:5000/"; 
    }); 

    // Add all the external providers you need before registering OpenIddict: 
    app.UseGoogleAuthentication(options => 
    { 
     options.AutomaticAuthenticate = true; 
     //options.AutomaticChallenge = true; 
     options.ClientId = "XXX"; 
     options.ClientSecret = "XXX"; 
    }); 
    //app.UseFacebookAuthentication(); 

    app.UseOpenIddict(); 

    // Enable all static file middleware 
    app.UseStaticFiles(); 

    // Enable Mvc for view controller, and 
    // default all routes to the Home controller 
    app.UseMvc(options => 
    { 
     options.MapRoute(
      name: "default", 
      template: "{*url}", 
      defaults: new { controller = "Home", action = "Index" }); 
    }); 
} 

AuthController.cs

public class AuthController : Controller 
{ 
    private UserManager<ApplicationUser> _userManager; 
    private SignInManager<ApplicationUser> _signInManager; 
    private ApplicationDbContext _applicationDbContext; 

    public AuthController(
     UserManager<ApplicationUser> userManager, 
     SignInManager<ApplicationUser> signInManager, 
     ApplicationDbContext applicationDbContext) 
    { 
     _userManager = userManager; 
     _signInManager = signInManager; 
     _applicationDbContext = applicationDbContext; 
    } 

    [HttpPost("google")] 
    public async Task<IActionResult> GoogleAsync([FromBody] ExternalLoginModel model) 
    { 
     // THIS IS WHERE ERROR OCCURS 
     var info = await _signInManager.GetExternalLoginInfoAsync(); 


     return Ok(); 
    } 
} 

ExternalLoginModel.cs

public class ExternalLoginModel 
{ 
    public string Code { get; set; } 

    public string ClientId { get; set; } 

    public string RedirectUri { get; set; } 
} 
+2

Mögliches Duplikat von [Kein Authentifizierungshandler ist für die Behandlung des Schemas konfiguriert: Automatisch] (https://stackoverflow.com/questions/33825058/no-authentication-handler-is-configured-to-handle-the-scheme- automatisch) –

Antwort

14

Haben Sie versucht, indem die Identität Middleware-Registrierung app.UseIdentity(); hinzufügen, bevor Sie die GoogleAuthentication Middleware registrieren ?

+1

Sie haben Recht, wenn ich das hinzufügen, funktioniert es gut. Aber das verwendet Cookies, wie kann ich keine Cookies verwenden? – Gillardo

+0

Es scheint, Cookies zu verwenden, und ich schätze Ihren anfänglichen Kommentar, der besagt, dass Sie sie nicht verwenden möchten. Die von Ihnen aufgerufene Methode 'GetExternalLoginInfoAsync();' verwendet jedoch die Cookie-Authentifizierung, wenn sie diesen Aufruf ausführt 'var auth = new AuthenticateContext (Options.Cookies.ExternalCookieAuthenticationScheme); '. Sie finden diesen Code hier: [SignInManager.cs] (https://github.com/aspnet/Identity/blob/58b2cf6c7dc946d0fce27f1fda150bbeb0e1a1fd/src/Microsoft.AspNet.Identity/SignInManager.cs). –

+0

Haben Sie sich Pinpoints anderes Framework [ASOS] (https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server) angesehen? Ich benutze es gerade und habe bis jetzt eine gute Erfahrung. Ich glaube auch, dass es für Ihre Anforderungen besser geeignet wäre. Momentan gibt es auf StackOverflow noch viel mehr Dokumentation, openiddict ist im Vergleich dazu noch relativ jung. –

Verwandte Themen