简体   繁体   English

Blazor WebAssembly 应用程序 /authentication/logout 导致“尝试注销时出错:''”失败

[英]Blazor WebAssembly app /authentication/logout results in "There was an error trying to log you out: ''" fail

I created a Blazor WebAssembly app and when clicking on the logout link /authentication/logout I get redirected to /authentication/logout-failed?message=The%20logout%20was%20not%20initiated%20from%20within%20the%20page.我创建了一个 Blazor WebAssembly 应用程序,当单击注销链接/authentication/logout时,我被重定向到/authentication/logout-failed?message=The%20logout%20was%20not%20initiated%20from%20within%20the%20page. with the message:与消息:

There was an error trying to log you out: ''尝试注销时出错:''

I'm using IdentityServer4.我正在使用 IdentityServer4。

How can I perform a proper logout and that I'm also logged out of the service providers (Facebook, Google and/or Microsoft)?我怎样才能执行正确的注销并且我也从服务提供商(Facebook、谷歌和/或微软)注销?

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddAuthentication()
                .AddFacebook(facebookOptions =>
                {
                    facebookOptions.ClientId = Configuration["Authentication:Facebook:ClientId"];
                    facebookOptions.ClientSecret = Configuration["Authentication:Facebook:ClientSecret"];
                })
                .AddGoogle(googleOptions =>
                {
                    googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
                    googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                })
                .AddMicrosoftAccount(microsoftOptions =>
                {
                    microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ClientId"];
                    microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"];
                });

            services.Configure<IdentityOptions>(options =>
                options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);

            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

            services.AddAuthentication()
                .AddIdentityServerJwt();
            services.AddControllersWithViews();
            services.AddRazorPages();
            services.AddOptions();
            services.AddAutoMapper(typeof(Startup));
            services.AddTransient<IEmailSender, EmailSender>();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
                app.UseWebAssemblyDebugging();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

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

            app.UseRouting();

            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapFallbackToFile("index.html");
            });
        }

In the meanwhile I found the solution.与此同时,我找到了解决方案。 In stead of directly linking to /authentication/logout/ I use the @onclick="BeginSignOut" on the logout link/button.我没有直接链接到/authentication/logout/ ,而是在注销链接/按钮上使用@onclick="BeginSignOut"

Then you need to inject:然后你需要注入:

        [Inject] NavigationManager Navigation { get; set; }
        [Inject] SignOutSessionStateManager SignOutManager { get; set; }

and use it in:并将其用于:

        protected async Task BeginSignOut(MouseEventArgs args)
        {
            await SignOutManager.SetSignOutState();
            Navigation.NavigateTo("authentication/logout");
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “尝试登录时出错:&#39;&#39;” Blazor WebAssembly 使用 IdentiyServer 身份验证 - "There was an error trying to log you in: '' " Blazor WebAssembly using IdentiyServer Authentication 将 Azure Active Directory 身份验证添加到 Blazor WebAssembly 应用程序 - Adding Azure Active Directory authentication to a Blazor WebAssembly app 在 .NET6 Blazor Webassembly 应用程序中使用外部程序集构建错误 - Build Error with External Assembly in .NET6 Blazor Webassembly App 在 Blazor Webassembly 中显示应用程序版本 - Display app version in Blazor Webassembly Blazor WASM Net 6 Preview 4 Azure AD - 尝试登录时出错:'无法读取未定义的属性'toLowerCase' - Blazor WASM Net 6 Preview 4 Azure AD - There was an error trying to log you in: 'Cannot read property 'toLowerCase' of undefined' 在具有 .NET 核心的 Blazor WebAssembly 中使用 Windows 身份验证 - Using Windows Authentication in Blazor WebAssembly with .NET Core blazor webassembly windows 身份验证 + .netcore 托管 - blazor webassembly windows authentication + .netcore hosted .NET 5 Blazor 服务器端和 WebAssembly(混合)与 WebAPI 身份验证? - .NET 5 Blazor ServerSide AND WebAssembly (hybrid) with WebAPI Authentication? Blazor WebAssembly 中 Window /Tab 关闭事件上的自动注销用户 - Automatic logout user on Window /Tab Close Event in Blazor WebAssembly Blazor Webassembly 应用程序显示来自服务器的图像 - Blazor Webassembly App display image from server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM