【问题标题】:Server Side Blazor + Kestrel + Windows Auth = Crash服务器端 Blazor + Kestrel + Windows Auth = 崩溃
【发布时间】:2019-03-26 14:24:19
【问题描述】:

我正在尝试在 blazor 服务器端使用 kesterel 设置 windows Auth。我的程序设置如下: 我正在使用 Blazor 服务器端的 0.6.0 版和 VS 2017 的最新版本。 使用开箱即用的 blazor 模板。您可以看到我在“Program.cs”的 BuildWebHost 中启用了“windows Auth”。如果我注释掉 options.Authentication.AllowAnonymous 行,当然一切正常。

Blazor.Web.server

程序.cs

public static void Main(string[] args)
    {
        BuildWebHost(args).Run();            
    }
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .UseHttpSys(
                                    options =>
                                    {
                                        options.Authentication.Schemes =
                                           AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM; 
                                        options.Authentication.AllowAnonymous = false;                                          
                                    })
            .UseConfiguration(new ConfigurationBuilder()
                .AddCommandLine(args)
                .Build())
            .UseStartup<Startup>()
            .Build();
}

启动.cs

public class Startup
            {
            public void ConfigureServices(IServiceCollection services)
            {
            // Since Blazor is running on the server, we can use an application 
            service
            // to read the forecast data.
            services.AddSingleton();
            }
            public void Configure(IBlazorApplicationBuilder app)
            {
            app.AddComponent("app");
            }
        }

程序.cs

public class Program
    { 
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }   

        public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) =>
            BlazorWebAssemblyHost.CreateDefaultBuilder()
                .UseBlazorStartup<Startup>();
    }

public class HttpContextAccessor
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public HttpContextAccessor(IHttpContextAccessor httpContextAccessor)
        {
               _httpContextAccessor = httpContextAccessor;
        }

        public HttpContext Context => _httpContextAccessor.HttpContext;
    }

Auth.cshtml

using System.Net.Http
@Inject Blazor.Web.App.HttpContextAccessor HttpContext
@page "/two-way-data-binding"

Logged in User: @HttpContext.Context.User.Identity.Name 

导航到 Auth.cshtml 时出现以下错误

System.ObjectDisposedException
HResult=0x80131622
Message=Safe handle has been closed
Source=System.Private.CoreLib
StackTrace:
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Interop.Advapi32.GetTokenInformation(SafeAccessTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeAccessTokenHandle tokenHandle, TokenInformationClass tokenInformationClass, Boolean nullOnInvalidParam)
at System.Security.Principal.WindowsIdentity.get_User()
at System.Security.Principal.WindowsIdentity.b__46_0()
at System.Security.Principal.WindowsIdentity.<>c__DisplayClass62_0.b__0(Object )
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Security.Principal.WindowsIdentity.RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)
at System.Security.Principal.WindowsIdentity.RunImpersonated(SafeAccessTokenHandle safeAccessTokenHandle, Action action)
at System.Security.Principal.WindowsIdentity.GetName()
at System.Security.Principal.WindowsIdentity.get_Name()
at Cloud.WebUI.App.Pages.TwoWayDataBinding.BuildRenderTree(RenderTreeBuilder builder)
at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.ProcessRenderQueue()
at Microsoft.AspNetCore.Blazor.Rendering.Renderer.AddToRenderQueue(Int32 componentId, RenderFragment renderFragment)

这也被创建为“问题”@Blazor Issue # 1596

【问题讨论】:

  • 伙计,您的代码格式错误,缺少许多部分。我猜您不仅是 Blazor 的新手,而且是 ASP.NET Core 的新手,对吧?如果你是 ASP.NET 开发人员,学习 Blazor 的第一步是学习 Core 风格。这是另一回事,相信我。当你对这个框架有一个合理的命令时,你的代码不会包含这样的东西:services.AddSingleton(); Blazor 将处于其最后的发展塑造阶段,您无需学习过时的东西。希望这会有所帮助。
  • 抱歉,这是默认的 blazor 模板。我从 DI 中删除了“天气预报服务”,因为它没有增加价值。除了“HttpContaxtAccessor”部分之外,大部分代码都是样板代码。看起来 blazor 没有通过 signalR 将用户上下文传递给服务器,或者它可能被过早地处理掉了。

标签: windows-authentication kestrel-http-server blazor


【解决方案1】:

首先,不需要您的自定义类 HttpContextAccessor,因为它不会增加任何价值。

确保将以下行添加到 服务器的 Startup.cs

ConfigureServices 方法中
services.AddHttpContextAccessor();

那么您就可以了,可以通过以下方式在您的 Blazor 应用中使用 HttpContextAccessor

@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
User: @HttpContextAccessor.HttpContext.User.Identity.Name

在您的服务器项目 Web 服务器设置(调试设置中的最后一个区域)中将 Windows 身份验证设置为 true。

我正在使用 Blazor 0.7.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2022-07-14
    • 2018-01-18
    • 2021-05-28
    • 2020-05-15
    • 2019-10-29
    • 2021-03-23
    • 2015-11-17
    相关资源
    最近更新 更多