I am creating an api using asp.net 5 rc2. I am trying to implement openiddict-core, found it here for local accounts, and also want to allow users to use external logins, for example google.
I installed all of this, but when I try to authenticate with Google and I call this code
var info = await _signInManager.GetExternalLoginInfoAsync();
I get an error in the header.
On the client, I use the Satellizer, which I found here , which takes care of opening the google help window and sending a callback to my Google AuthController method, which is the normal ChallengeResult code that you see in other mvc6 examples.
I wrote the code to get user information manually, and it works, but I thought that instead I would use the already built signInManager, instead of playing the wheel ...
Maybe I didn’t set the right settings, since all the examples seem to use cookies, which I believe are related to the fact that they are mvc6 web applications and not api. I do not want to use cookies, but this may 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) {
ExternalLoginModel.cs
public class ExternalLoginModel { public string Code { get; set; } public string ClientId { get; set; } public string RedirectUri { get; set; } }