没有配置身份验证处理程序来验证该方案:Microsoft.AspNet.Identity.External

Gil*_*rdo 5 .net c# asp.net authentication asp.net-core

我正在使用asp.net 5 rc2构建一个api.我正在尝试实现openiddict-core,在这里找到本地帐户,我也想让用户使用外部登录,例如谷歌.

我已经设置了所有,但当我尝试实施谷歌身份验证,我称之为代码

var info = await _signInManager.GetExternalLoginInfoAsync();

我在标题中收到错误消息.

在客户端,我使用Satellizer发现在这里,这需要打开谷歌提示窗口的关怀和发送回调到我的AuthController谷歌的方法,这是你看到正常的ChallengeResult代码在其他mvc6例子.

我已经编写了代码来手动获取用户详细信息,但是我认为我会使用已经构建的signInManager,而不是重现轮...

我可能没有正确设置,因为所有示例似乎都使用cookie,我猜测是因为它们是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" });
    });
}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

ExternalLoginModel.cs

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

    public string ClientId { get; set; }

    public string RedirectUri { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Ian*_*uty 14

您是否尝试app.UseIdentity();在注册GoogleAuthentication中间件之前添加注册Identity中间件?