没有认证处理程序配置方案进行验证:Microsoft.AspNet.Identity.External [英] No authentication handler is configured to authenticate for the scheme: Microsoft.AspNet.Identity.External

查看:305
本文介绍了没有认证处理程序配置方案进行验证:Microsoft.AspNet.Identity.External的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net 5 RC2构建的API。我想实现openiddict核,发现这里本地帐户,我也希望允许用户使用外部登录,如谷歌。

我已经设置了这一切,但是当我尝试实施谷歌认证,我称之为code

VAR信息=等待_signInManager.GetExternalLoginInfoAsync();

我得到在标题中错误消息。

在客户端我使用Satellizer发现这里,这需要打开谷歌提示窗口的关怀和发送回调到我的AuthController谷歌的方法,这是正常的ChallengeResult code,你在其他mvc6例子中看到。

我已经写了手动code,以获得用户的详细信息和这样的作品,但我想我会用已建signInManager代替,而不是再现轮...

我可能设置不正确的事情了,因为所有的例子似乎使用cookies,我猜是因为他们mvc6 Web应用程序,而不是一个API。我不想使用cookies,但是这可能是我的问题。

现在一些code。

startup.cs

 公共无效ConfigureServices(IServiceCollection服务)
{
    //到服务容器添加MVC服务。
    services.AddMvc();    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext< ApplicationDbContext>(选项=>
        options.UseSqlServer(_​​configuration [数据:DefaultConnection:ConnectionString的]));    services.AddIdentity< ApplicationUser,IdentityRole>()
        .AddEntityFrameworkStores< ApplicationDbContext>()
        .AddDefaultTokenProviders()
        .AddOpenIddict(); //注册身份服务后添加OpenIddict服务。
}公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment ENV,ILoggerFactory的LoggerFactory)
{
    //使用智威汤逊承载认证
    app.UseJwtBearerAuthentication(选项=>
    {
        options.AutomaticAuthenticate = TRUE;
        options.AutomaticChallenge = TRUE;
        options.RequireHttpsMetadata = FALSE;
        options.Audience =HTTP://本地主机:5000 /;
        options.Authority =HTTP://本地主机:5000 /;
    });    //添加你注册OpenIddict之前需要外部供应商:
    app.UseGoogleAuthentication(选项=>
    {
        options.AutomaticAuthenticate = TRUE;
        //options.AutomaticChallenge = TRUE;
        options.ClientId =XXX;
        options.ClientSecret =XXX;
    });
    //app.UseFacebookAuthentication();    app.UseOpenIddict();    //启用所有静态文件的中间件
    app.UseStaticFiles();    //启用的MVC视图控制器,并
    //默认情况下所有航线的Home控制器
    app.UseMvc(选项=>
    {
        options.MapRoute(
            名称:默认,
            模板:{* URL},
            默认:新{控制器=家,行动=索引});
    });
}

AuthController.cs

 公共类AuthController:控制器
{
    私人的UserManager< ApplicationUser> _userManager;
    私人SignInManager< ApplicationUser> _signInManager;
    私人ApplicationDbContext _applicationDbContext;    公共AuthController(
        的UserManager< ApplicationUser>的UserManager,
        SignInManager< ApplicationUser> signInManager,
        ApplicationDbContext applicationDbContext)
    {
        _userManager =的UserManager;
        _signInManager = signInManager;
        _applicationDbContext = applicationDbContext;
    }    [HttpPost(谷歌)
    公共异步任务< IActionResult> GoogleAsync([FromBody] ExternalLoginModel模型)
    {
        //这是错误发生时
        VAR信息=等待_signInManager.GetExternalLoginInfoAsync();
        返回OK();
    }
}

ExternalLoginModel.cs

 公共类ExternalLoginModel
{
    公共字符串code {搞定;组; }    公共字符串客户端Id {获取;组; }    公共字符串RedirectUri {搞定;组; }
}


解决方案

您是否尝试过通过增加注册身份中间件 app.UseIdentity(); 您注册GoogleAuthentication前中间件?

I am building an api using asp.net 5 rc2. I am trying to implement openiddict-core, found here for local accounts and i also want to allow for users to use external logins, such as google.

I have set it all up, but when i try to implement google authentication, and i call this code

var info = await _signInManager.GetExternalLoginInfoAsync();

I get the error message in the title.

On the client i am using Satellizer found here, which takes care of opening the google prompt window and send the callback to my AuthController Google method, which is the normal ChallengeResult code you see in other mvc6 examples.

I have wrote code to get the users details manually and that works, but i thought i would use the already built signInManager instead, rather than reproducing the wheel...

I may have not set things up correctly, as all the examples seem to use cookies, which i am guessing is because they are mvc6 web applications, not an api. I do not want to use cookies, but this could 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)
    {
        // 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; }
}

解决方案

Have you tried registering the Identity middleware by adding app.UseIdentity(); before you register the GoogleAuthentication middleware?

这篇关于没有认证处理程序配置方案进行验证:Microsoft.AspNet.Identity.External的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆