我正在尝试在blazer服务器端使用kesterel设置Windows Auth。我有这样的程序设置: 我使用的是 Blazor 服务器端的 0.6.0 版和最新版本的 VS 2017。 使用开箱即用的西装外套板。您可以看到我在"Program.cs"的BuildWebHost中启用了"windows Auth"。如果我注释掉选项。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 问题 # 1596 创建的
首先,不需要自定义类HttpContextAccessor,因为它不会添加任何值。
请务必将以下行添加到服务器启动的配置服务方法中.cs
services.AddHttpContextAccessor();
然后,你可以通过以下方式在 Blazor 应用中使用HttpContextAccessor:
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
User: @HttpContextAccessor.HttpContext.User.Identity.Name
在服务器的项目Web 服务器设置(调试设置中的最后一个区域(中将 Windows 身份验证设置为 true。
我正在使用 Blazor 0.7.0