首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发布到Azure后的Azure SignalR服务403

发布到Azure后的Azure SignalR服务403
EN

Stack Overflow用户
提问于 2022-04-06 18:47:24
回答 2查看 461关注 0票数 1

最近,我开始使用.Net Core6Bazor服务器聊天应用程序signalR进行实验。我能够让示例应用程序在本地工作。然而,通过发布的Azure应用服务将该应用程序迁移到Azure SignalR服务一直是一个难题。

我一直得到403个被禁止的结果。

在使用.net核心6和SignalR服务的教程中,我没有发现太多。我欢迎一步一步的链接!

Program.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
@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 (禁止)。

我不知道我还能提供哪些其他信息来帮助解决这个问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-15 15:40:48

结果发现这个问题与身份验证有关。我的App已经启用了身份验证,但是我发布的应用程序目前还没有使用身份验证。我在App设置中关闭了身份验证(目前)并重新发布。现在起作用了。

票数 0
EN

Stack Overflow用户

发布于 2022-04-07 10:10:40

我想您需要从App配置中获得web套接字功能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71772155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档