Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No authentication handler is configured to authenticate for the scheme: Microsoft.AspNet.Identity.External

I am building an api using asp.net 5 rc2. I am trying to implement openiddict-core, found here for local accounts and i also want to allow for users to use external logins, such as google.

I have set it all up, but when i try to implement google authentication, and i call this code

var info = await _signInManager.GetExternalLoginInfoAsync();

I get the error message in the title.

On the client i am using Satellizer found here, which takes care of opening the google prompt window and send the callback to my AuthController Google method, which is the normal ChallengeResult code you see in other mvc6 examples.

I have wrote code to get the users details manually and that works, but i thought i would use the already built signInManager instead, rather than reproducing the wheel...

I may have not set things up correctly, as all the examples seem to use cookies, which i am guessing is because they are mvc6 web applications, not an api. I do not want to use cookies, but this could be my problem.

Now for some 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; }
}
like image 940
Gillardo Avatar asked Jan 13 '16 09:01

Gillardo


1 Answers

Have you tried registering the Identity middleware by adding app.UseIdentity(); before you register the GoogleAuthentication middleware?

like image 166
Ian Auty Avatar answered Oct 14 '22 00:10

Ian Auty