首页 > 解决方案 > 在服务器端 Blazor 中成功完成外部外部提供程序质询后登录用户

问题描述

我正在努力将我的服务器端 Web 应用程序配置为使用外部登录提供程序(在本例中为 Discord)登录。我正在为我的 oauth 提供程序使用https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers 。值得注意的是,此应用程序使用不和谐的外部提供程序,没有使用用户名/密码的常规登录功能。

这就是我在 Startup.cs 的 ConfigureServices 方法中设置身份验证、授权和不和谐 oauth 中间件的方法:

             services.AddIdentity<ApplicationUser, IdentityRole>(options =>
             {
                 options.User.RequireUniqueEmail = false;
                 options.Password.RequireDigit = false;
                 options.Password.RequiredLength = 4;
                 options.Password.RequiredUniqueChars = 0;
                 options.Password.RequireNonAlphanumeric = false;
                 options.Password.RequireLowercase = false;
                 options.Password.RequireUppercase = false;
                 options.Lockout.AllowedForNewUsers = false;
             }).AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddAuthorization();
            services.AddAuthentication().AddDiscord(x =>
            {
                x.ClientId = Configuration["AppId"];
                x.ClientSecret = Configuration["AppSecret"];
                x.Scope.Add("guilds");
                x.Scope.Add("identify");
                x.Validate();
            });

这就是我在我的配置方法中所拥有的

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // environment stuff

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });

        }

无论出于何种原因,每当我执行挑战(成功重定向到不和谐登录,然后重定向回 /sigin-discord)时,它都不会登录用户。

这是我处理登录的身份控制器,在挑战发生后,用户被发送到 /sigin-discord 然后重定向到 /sigin 以便登录用户或为他们创建一个帐户,如果他们不这样做'还没有。

        [HttpGet("login")]
        public async Task login(string redirectUrl)
        {
            string redirectUri = Url.Action("ExternalLoginCallback","Identity",new { returnUrl = redirectUrl});

            var properties = await signInManager.GetExternalAuthenticationSchemesAsync();

            await HttpContext.ChallengeAsync(properties.FirstOrDefault().Name, new AuthenticationProperties { RedirectUri = redirectUri });
        }

        [HttpGet("signin")]
        public async Task ExternalLoginCallback(string returnUrl)
        {
            returnUrl = returnUrl ?? "/";
            var info = await signInManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                HttpContext.Response.Redirect(returnUrl);
            }
            var result = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, true);
            if (result.Succeeded)
            {
                HttpContext.Response.Redirect(returnUrl);
            }
            else
            {
                var username = info.Principal.FindFirstValue(ClaimTypes.Name);
                if (username != null)
                {
                    var user = await UserManager.FindByNameAsync(info.Principal.Identity.Name);
                    if (user == null)
                    {
                        user = new ApplicationUser(info.Principal.FindFirstValue(ClaimTypes.Name), Convert.ToUInt64(info.ProviderKey));
                        await UserManager.CreateAsync(user);
                    }
                    await UserManager.AddLoginAsync(user, info);
                    await signInManager.SignInAsync(user, true);
                }
                HttpContext.Response.Redirect(returnUrl);
            }

然而。即使我成功完成了挑战,signInManager.GetExternalLoginInfoAsync() 始终返回 null。

有没有人尝试过使用带有 blazor 的外部登录提供程序?如果是这样,有没有人知道为什么这不起作用?

标签: asp.net-coreblazorblazor-server-side

解决方案


推荐阅读