最近,我开始使用.Net Core6Bazor服务器聊天应用程序signalR进行实验。我能够让示例应用程序在本地工作。然而,通过发布的Azure应用服务将该应用程序迁移到Azure SignalR服务一直是一个难题。
我一直得到403个被禁止的结果。
在使用.net核心6和SignalR服务的教程中,我没有发现太多。我欢迎一步一步的链接!
Program.cs
using Microsoft.AspNetCore.ResponseCompression;
using RunOn.Hubs;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using RunOn.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddSignalR().AddAzureSignalR();
builder.Services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
var app = builder.Build();
app.UseResponseCompression();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapHub<ChatHub>("/chathub");
app.MapFallbackToPage("/_Host");
app.Run();Index.razr
@page "/"
@using Microsoft.AspNetCore.SignalR.Client
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime
@implements IAsyncDisposable
<PageTitle>Index</PageTitle>
<div class="form-group">
<label>
User:
<input @bind="userInput" />
</label>
</div>
<div class="form-group">
<label>
Message:
<input @bind="messageInput" size="50" />
</label>
</div>
<button @onclick="Send" disabled="@(!IsConnected)">Send</button>
<hr>
<ul id="messagesList">
@foreach (var message in messages)
{
<li>@message</li>
}
</ul>
@code {
private HubConnection? hubConnection;
private List<string> messages = new List<string>();
private string? userInput;
private string? messageInput;
protected override async Task OnInitializedAsync()
{
hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/chathub"))
.Build();
hubConnection.On<string, string>("ReceiveMessage", (user, message) =>
{
var encodedMsg = $"{user}: {message}";
messages.Add(encodedMsg);
StateHasChanged();
});
await hubConnection.StartAsync();
}
private async Task Send()
{
if (hubConnection is not null)
{
await hubConnection.SendAsync("SendMessage", userInput, messageInput);
}
}
public bool IsConnected =>
hubConnection?.State == HubConnectionState.Connected;
public async ValueTask DisposeAsync()
{
if (hubConnection is not null)
{
await hubConnection.DisposeAsync();
}
}
}下面是错误: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware1,在执行请求时发生了一个未处理的异常。System.Net.Http.HttpRequestException:响应状态代码并不表示成功: 403 (禁止)。
我不知道我还能提供哪些其他信息来帮助解决这个问题。
发布于 2022-04-15 15:40:48
结果发现这个问题与身份验证有关。我的App已经启用了身份验证,但是我发布的应用程序目前还没有使用身份验证。我在App设置中关闭了身份验证(目前)并重新发布。现在起作用了。
发布于 2022-04-07 10:10:40
我想您需要从App配置中获得web套接字功能。

https://stackoverflow.com/questions/71772155
复制相似问题