2016-01-13 66 views
5

我正在构建使用asp.net 5 rc2 api。我试图执行openiddict-core,发现本地帐户为here,我也希望允许用户使用外部登录名,例如google。没有身份验证处理程序配置为验证该方案:Microsoft.AspNet.Identity.External

我已经设置了这一切,但是当我尝试实施谷歌认证,并且我称这种代码

var info = await _signInManager.GetExternalLoginInfoAsync();

我得到的称号错误消息。

在客户端上,我使用了Satellizer找到的here,它负责打开Goog​​le提示窗口并将回调发送到我的AuthController Google方法,这是您在其他mvc6示例中看到的正常ChallengeResult代码。

我已经写代码来获取用户手工细节和工作,但我想我会用已建signInManager代替,而不是再现轮...

我可能没有正确设置好了,因为所有的例子似乎都使用cookies,我猜测这是因为它们是mvc6 web应用程序,而不是api。我不想使用cookies,但这可能是我的问题。

现在有些代码。

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

​​

ExternalLoginModel.cs

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

    public string ClientId { get; set; } 

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

可能重复[没有认证处理程序配置为处理该方案:自动](https://stackoverflow.com/questions/33825058/no-authentication-handler-is-configured-to-handle-the-scheme-自动) –

回答

14

您是否尝试过通过增加app.UseIdentity();注册身份中间件您注册GoogleAuthentication中间件之前?

+1

你是对的,如果我添加它,它工作正常。但是这使用cookie,我怎么不使用cookie? – Gillardo

+0

它似乎使用cookies,我很欣赏你的初步评论,表示你不想使用它们。但是,在这里调用的方法'GetExternalLoginInfoAsync();'在进行此调用时使用cookie身份验证'var auth = new AuthenticateContext(Options.Cookies.ExternalCookieAuthenticationScheme);'。你可以在这里找到这个代码:[SignInManager.cs](https://github.com/aspnet/Identity/blob/58b2cf6c7dc946d0fce27f1fda150bbeb0e1a1fd/src/Microsoft.AspNet.Identity/SignInManager.cs)。 –

+0

你有没有看过Pinpoint的其他框架[ASOS](https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server)?目前我正在使用它,并且迄今为止有很好的体验。我也相信它会更适合你的要求。目前在StackOverflow上还有很多文档,openiddict相对来说还是比较年轻的。 –

相关问题