服务器端Blazor + Kestrel + Windows Auth =崩溃

时间:2018-10-22 13:26:47

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

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

Blazor.Web.server

Program.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();
}

Startup.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");
            }
        }

Program.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

1 个答案:

答案 0 :(得分:0)

首先,不需要自定义类 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