5

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; }
}
Gillardo
  • 9,518
  • 18
  • 73
  • 141
  • 2
    Possible duplicate of [No authentication handler is configured to handle the scheme: Automatic](https://stackoverflow.com/questions/33825058/no-authentication-handler-is-configured-to-handle-the-scheme-automatic) – Michael Freidgeim Oct 05 '17 at 13:08

1 Answers1

14

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

Ian Auty
  • 847
  • 7
  • 10
  • 1
    You are right, if i add this it works fine. But this uses cookies, how can i not use cookies?? – Gillardo Jan 14 '16 at 14:45
  • It does appear to use cookies and I appreciate your initial comment stated that you don't want to use them. However, the method you're calling here `GetExternalLoginInfoAsync();` makes use of cookie authentication when it makes this call `var auth = new AuthenticateContext(Options.Cookies.ExternalCookieAuthenticationScheme);`. You can find that code here: [SignInManager.cs](https://github.com/aspnet/Identity/blob/58b2cf6c7dc946d0fce27f1fda150bbeb0e1a1fd/src/Microsoft.AspNet.Identity/SignInManager.cs). – Ian Auty Jan 15 '16 at 14:14
  • Have you taken a look at Pinpoint's other framework [ASOS](https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server)? I'm using it currently and am having a good experience so far. I also believe it would be more suitable for your requirements. There is also a lot more documentation for it currently on StackOverflow, openiddict is still relatively young in comparison. – Ian Auty Jan 15 '16 at 14:26
  • 1
    Does this sound like the sort of thing you're looking to do [link](http://stackoverflow.com/questions/33044879/is-it-possible-to-use-an-external-identity-provider-in-a-web-api-with-asp-net-5/33148160#33148160) – Ian Auty Jan 15 '16 at 14:45