【发布时间】:2016-01-13 09:50:22
【问题描述】:
我正在使用 asp.net 5 rc2 构建一个 api。我正在尝试实现 openiddict-core,为本地帐户找到 here,我还希望允许用户使用外部登录,例如 google。
我已经全部设置好了,但是当我尝试实现 google 身份验证时,我调用了这个代码
var info = await _signInManager.GetExternalLoginInfoAsync();
我在标题中收到错误消息。
在客户端上,我使用的是 Satellizer,找到了 here,它负责打开 google 提示窗口并将回调发送到我的 AuthController Google 方法,这是您在其他 mvc6 示例中看到的正常 ChallengeResult 代码。
我已经编写了代码来手动获取用户详细信息并且可以正常工作,但我想我会改用已经构建的 signInManager,而不是复制轮子...
我可能没有正确设置,因为所有示例似乎都使用 cookie,我猜这是因为它们是 mvc6 Web 应用程序,而不是 api。我不想使用 cookie,但这可能是我的问题。
现在是一些代码。
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; }
}
【问题讨论】:
标签: c# asp.net .net authentication asp.net-core