【问题标题】:No authentication handler is configured to authenticate for the scheme: Microsoft.AspNet.Identity.External没有配置身份验证处理程序来对方案进行身份验证:Microsoft.AspNet.Identity.External
【发布时间】: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


【解决方案1】:

您是否尝试在注册 GoogleAuthentication 中间件之前通过添加 app.UseIdentity(); 来注册 Identity 中间件?

【讨论】:

  • 你是对的,如果我添加它它工作正常。但这使用cookies,我怎么能不使用cookies??
  • 它似乎确实使用了 cookie,我感谢您最初的评论指出您不想使用它们。但是,您在此处调用的方法GetExternalLoginInfoAsync(); 在调用var auth = new AuthenticateContext(Options.Cookies.ExternalCookieAuthenticationScheme); 时使用了cookie 身份验证。您可以在此处找到该代码:SignInManager.cs.
  • 你看过Pinpoint的另一个框架ASOS吗?我目前正在使用它,到目前为止我的体验很好。我也相信它会更适合您的要求。目前在 StackOverflow 上还有很多关于它的文档,相比之下 openiddict 还比较年轻。
  • 这听起来像你想做的事情吗link
猜你喜欢
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多