Blazor Server和Signalr和Azure AD

发布于 2025-02-12 01:52:17 字数 1953 浏览 2 评论 0原文

我正在使用Blazor Server .NET 5进行Web应用程序工作。在我的索引页面上,我需要向通过Azure AD登录到网站的在线用户数量。

首先,用户访问网络,并将其重定向到Azure AD。用户在广告中进行身份验证后,他/她将降落在索引页面上。我想显示应用程序中的在线用户数量。我开始使用SignalR,但是我遇到了一个非常奇怪的错误。

我正在使用Singalr客户端lib 首先,我

PeoplHub : Hub{
public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }

}

在我的索引中创建了当时的。我创建了

hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
            .Build();

        hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
        {
            var encodedMsg = $"{user}: {message}";
            messages.Add(encodedMsg);
            InvokeAsync(StateHasChanged);
        });
         await hubConnection.StartAsync();

实现了iaSyncdisposal ,我会收到此错误消息

 public async ValueTask DisposeAsync()
    {
        if (hubConnection is not null)
        {
            await hubConnection.DisposeAsync();
        }
    }

实现的启动中

services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
app.UseResponseCompression();

endpoints.MapHub<PeopleHub>("/peoplehub");

我还在运行应用程序时

An unhandled exception occurred while processing the request.
JsonReaderException: '<' is an invalid start of a value. LineNumber: 2 | BytePositionInLine: 0.
System.Text.Json.ThrowHelper.ThrowJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan<byte> bytes)

InvalidDataException: Invalid negotiation response received.
Microsoft.AspNetCore.Http.Connections.NegotiateProtocol.ParseResponse(ReadOnlySpan<byte> content)

I am working on a web application using Blazor Server .Net 5. On my index page, I need to show the number of online users that logged into the website through Azure AD.

First, the user hits the web, and it gets redirected to Azure AD. Once the user is Authenticated in AD he/she will land on the index page. I want to show number of online users inside the app. I started using SignalR, but I am getting a very weird Error.

I am using SingalR client lib
First I created the

PeoplHub : Hub{
public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }

}

Then in my Index.razor I have created

hubConnection = new HubConnectionBuilder()
            .WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
            .Build();

        hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
        {
            var encodedMsg = 
quot;{user}: {message}";
            messages.Add(encodedMsg);
            InvokeAsync(StateHasChanged);
        });
         await hubConnection.StartAsync();

I have also Implemented the IAsyncDisposal

 public async ValueTask DisposeAsync()
    {
        if (hubConnection is not null)
        {
            await hubConnection.DisposeAsync();
        }
    }

in my startup I implemented

services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
app.UseResponseCompression();

endpoints.MapHub<PeopleHub>("/peoplehub");

When I run the app, I get this error message

An unhandled exception occurred while processing the request.
JsonReaderException: '<' is an invalid start of a value. LineNumber: 2 | BytePositionInLine: 0.
System.Text.Json.ThrowHelper.ThrowJsonReaderException(ref Utf8JsonReader json, ExceptionResource resource, byte nextByte, ReadOnlySpan<byte> bytes)

InvalidDataException: Invalid negotiation response received.
Microsoft.AspNetCore.Http.Connections.NegotiateProtocol.ParseResponse(ReadOnlySpan<byte> content)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者 2025-02-19 01:52:17

在研究了这个问题之后。我找到了一些有用的信息。我们不知道已知问题,您可以创建一个支持票并寻求帮助。

事实证明,有一个已知的问题打破了带有Blazor Server和Microsoft Identity的Signalr Hub。

而且我还发现官方工程师说,鉴于我们没有看到很多客户击中它,因此他们不打算在这一领域进行改进。

相关问题:

blazor serverr signalr jsonreaderexception

asp.net core blazor服务器其他安全方案

After researching on this issue. I found some useful information. We don't know the known issue, you can create a support ticket and ask for help.

It turns out that there is a known issue breaking SignalR Hubs with Blazor Server and Microsoft Identity.

And I also find official engineer said they don't plan to make improvements in this area given that we haven't seen many customers hitting it.

Related Issue:

blazor server signalr JsonReaderException

Workaround

ASP.NET Core Blazor Server additional security scenarios

同展鸳鸯锦 2025-02-19 01:52:17

加上Jason Pan的答案。

验证授权的快速方法是问题。

由于我知道我的代码在dotnet 7应用中无授权的情况下起作用
当我将代码移至生产代码中时,发现了这个错误(dotnet 6)
在我们使用Azure AD

的授权的情况下,我在集线器上进行了“允许的”测试。

[AllowAnonymous()] //TODO: authorize...
    public class SignalrHub : Hub
    {

,一切都按预期工作。
下一步:遵循杰森(Jason)发布的解决方法

Adding on to the answer by Jason Pan.

A quick way to validate the authorization is the problem.

Since I knew my code worked without Authorization in a dotnet 7 app
and this error was seen when I moved the code into my production code (dotnet 6)
where we use authorization with Azure AD

I ran a test with "AllowAnymous" on the hub.

[AllowAnonymous()] //TODO: authorize...
    public class SignalrHub : Hub
    {

and everything works as expected.
Next : follow the workaround as posted by Jason

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文