3

我正在尝试在 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;
    }

验证.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 问题 # 1596

4

1 回答 1

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

于 2019-01-11T09:59:41.850 回答