我正在使用asp.net 5 rc2构建一个api。我正在尝试实施openiddict-core,为本地帐户找到here,我也想允许用户使用外部登录,例如google。
我已经设置了所有内容,但是当我尝试实施Google身份验证时,我调用此代码
var info = await _signInManager.GetExternalLoginInfoAsync();
我在标题中收到错误消息。
在客户端我正在使用找到here的Satellizer,它负责打开google提示窗口并将回调发送到我的AuthController Google方法,这是您在其他mvc6示例中看到的正常ChallengeResult代码。 / p>
我已经编写了代码来手动获取用户详细信息,但是我认为我会使用已经构建的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" });
});
}
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; }
}
答案 0 :(得分:14)
您是否尝试在注册GoogleAuthentication中间件之前添加app.UseIdentity();
来注册Identity中间件?