首页 > 解决方案 > ASP.NET Core: InvalidOperationException: 没有指定 authenticationScheme,也没有找到 DefaultSignInScheme

问题描述

我们有一个全新的 ASP.NET Core 站点,用于升级旧的 ASP.NET Framework 站点。那时我们使用 DotNetOpenAuth 进行 OpenID 登录,现在我们尝试在 ASP.NET Core 中进行复制。

我们已经将我们的站点重定向到 OpenID 提供程序,能够登录,但是在回调我们的站点时会引发异常:

InvalidOperationException: No authenticationScheme was specified, and there was no DefaultSignInScheme found.

我找不到一个完整的例子,这真的可以帮助我理解整个过程。我从各种来源收集了以下内容......

在 Startup 的 ConfigureServices 中:

            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                // Set a short timeout for easy testing.
                options.IdleTimeout = TimeSpan.FromSeconds(10);
                options.Cookie.HttpOnly = true;
                options.Cookie.SecurePolicy = CookieSecurePolicy.Always; //require https
                // Make the session cookie essential
                options.Cookie.IsEssential = true;
            });

            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false; //true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddSimpleInjector(_simpleInjectorContainer, options =>
            {
                // AddAspNetCore() wraps web requests in a Simple Injector scope.
                options.AddAspNetCore()
                    // Ensure activation of a specific framework type to be created by
                    // Simple Injector instead of the built-in configuration system.
                    .AddControllerActivation()
                    .AddViewComponentActivation();
                //.AddPageModelActivation()
                //.AddTagHelperActivation();
            });

            services.AddAuthentication(options =>
            { /* Authentication options */
              //options.DefaultAuthenticateScheme = "Steam";

            })
                .AddSteam(options =>
                {
                });

由于之前没有 ASP.NET Core 的经验,我盲目地尝试将“Steam”分配给DefaultAuthenticateSchemeinside of AddAuthentication,但这会引发错误,说它不能调用自身。

我们一直在使用默认的 Home 控制器作为测试场:

    [HttpGet("~/signin")]
    public IActionResult SignIn()
    {
        // Instruct the OIDC client middleware to redirect the user agent to the identity provider.
        // Note: the authenticationType parameter must match the value configured in Startup.cs
        return Challenge(new AuthenticationProperties
        {
            RedirectUri = Url.Action("HandleSteamLogin", "Home"),
        }, "Steam");
    }

    public async Task<IActionResult> HandleSteamLogin()
    {
        //Everything in this method is marked as obsolete, so it's a poor example.  I guess it's from an older version of ASP.NET Core?

        var claimsPrincipal = await HttpContext.Authentication.AuthenticateAsync("ExternalCookie");

        //do something the the claimsPrincipal, possibly create a new one with additional information
        //create a local user, etc

        await HttpContext.Authentication.SignInAsync("MainCookie", claimsPrincipal);
        await HttpContext.Authentication.SignOutAsync("ExternalCookie");
        return Redirect("~/");
    }

我什至可以在异常数据中看到,我们正在获取必要的登录信息,但我确实需要一个完整的示例才能理解。

标签: c#asp.net-coreoauth-2.0openid-connect

解决方案


推荐阅读