C# 没有为以下方案配置身份验证处理程序:Microsoft.AspNet.Identity.External
我正在使用asp.net 5 rc2构建一个api。我正在尝试实现openiddict core,这是为本地帐户找到的,我还希望允许用户使用外部登录,例如google 我已经设置好了,但是当我尝试实现google身份验证时,我调用了这个代码C# 没有为以下方案配置身份验证处理程序:Microsoft.AspNet.Identity.External,c#,asp.net,.net,authentication,asp.net-core,C#,Asp.net,.net,Authentication,Asp.net Core,我正在使用asp.net 5 rc2构建一个api。我正在尝试实现openiddict core,这是为本地帐户找到的,我还希望允许用户使用外部登录,例如google 我已经设置好了,但是当我尝试实现google身份验证时,我调用了这个代码 var info=await\u signInManager.getexternallogininfosync() 我在标题中得到了错误消息 在客户端上,我使用的是Satellizer found,它负责打开google提示符窗口并将回调发送给我的AuthC
var info=await\u signInManager.getexternallogininfosync()代码>
我在标题中得到了错误消息
在客户端上,我使用的是Satellizer found,它负责打开google提示符窗口并将回调发送给我的AuthController google方法,这是您在其他mvc6示例中看到的正常ChallengeResult代码
我已经编写了代码来手动获取用户详细信息,这是可行的,但我想我应该使用已经构建的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" });
});
}
您是否尝试通过添加app.UseIdentity()注册身份中间件
在注册GoogleAuthentication中间件之前?你说得对,如果我添加这个,它可以正常工作。但是这使用了cookies,我怎么能不使用cookies呢?它看起来确实使用了cookies,我很感激你最初的评论,你说你不想使用它们。但是,您在这里调用的方法是getexternallogininfosync()
在进行此调用时使用cookie身份验证var auth=new AuthenticateContext(Options.Cookies.ExternalCookieAuthenticationScheme)代码>。你可以在这里找到代码:。你看过Pinpoint的其他框架了吗?我目前正在使用它,到目前为止我有一个很好的经验。我也相信它会更适合你的要求。目前在StackOverflow上还有很多关于它的文档,相比之下openiddict还相对年轻。这听起来像是你想要做的事情吗
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();
}
}
public class ExternalLoginModel
{
public string Code { get; set; }
public string ClientId { get; set; }
public string RedirectUri { get; set; }
}