asp.net 通过信号集线器访问时,上下文,用户,标识,名称为空

yc0p9oo0  于 2023-06-25  发布在  .NET
关注(0)|答案(1)|浏览(109)

我正在尝试访问我的信号中心内的上下文。用户。身份。名称,但我一直得到空值。我在集线器类的顶部添加了[Authorize]属性,但现在我得到了401 Unauthorized错误。
此外,我正在使用asp.net Identity作为我的授权方法,当尝试与signalr服务器建立连接时,我的授权cookie似乎正在通过,但我仍然无法显示Context.User.Identity.Name。我做错了什么?
我的program.cs

global using BagiBagiDev.Shared;
global using BagiBagiDev.Server.Services.FollowerFollowingService;
global using BagiBagiDev.Server.Services.AuthService;
global using BagiBagiDev.Server.Services.UserService;
global using BagiBagiDev.Server.Services.TransactionService;
global using BagiBagiDev.Server.Services.DonatorService;
global using BagiBagiDev.Server.Services.SocialService;
global using BagiBagiDev.Server.Services.PaymentService;
global using BagiBagiDev.Server.Models;
global using BagiBagiDev.Server.Data;
global using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.AspNetCore.Identity;
using BagiBagiDev.Server;
using Duende.IdentityServer.Services;
using BagiBagiDev.Server.Services;
using BagiBagiDev.Server.Hubs;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddClaimsPrincipalFactory<ApplicationClaimsPrincipalFactory>();

builder.Services.AddIdentityServer()
    .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

builder.Services.AddAuthentication()
    .AddCookie(options =>
    {
        options.Cookie.Name = "BagiBagiAuth.Cookie";
        options.ExpireTimeSpan = TimeSpan.FromDays(1);
    })
    .AddIdentityServerJwt();
builder.Services.TryAddEnumerable(
    ServiceDescriptor.Singleton<IPostConfigureOptions<JwtBearerOptions>,
        ConfigureJwtBearerOptions>());

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

builder.Services.AddTransient<IProfileService, ProfileService>();

builder.Services.AddScoped<IFollowerFollowingService, FollowerFollowingService>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ITransactionService, TransactionService>();
builder.Services.AddScoped<IDonatorService, DonatorService>();
builder.Services.AddScoped<ISocialService, SocialService>();
builder.Services.AddScoped<IPaymentService, PaymentService>();

builder.Services.AddSwaggerGen();

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

builder.Services.AddSignalR();
builder.Services.AddSingleton<IUserIdProvider, NameUserIdProvider>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
    app.UseWebAssemblyDebugging();
    app.UseSwagger();
    app.UseSwaggerUI();
}
else
{
    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.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();

app.UseResponseCompression();

app.UseIdentityServer();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllers();
    endpoints.MapHub<PaymentHub>("/ws/payment");
});
app.MapFallbackToFile("index.html");

app.Run();

这是我的PaymentHub.cs

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;

namespace BagiBagiDev.Server.Hubs
{
    [Authorize]
    public class PaymentHub : Hub
    {
        public async Task PaymentChange(string userId)
        {
            await Clients.All.SendAsync("Test", userId);
        }

        public override Task OnConnectedAsync()
        {
            return base.OnConnectedAsync();
        }
    }
}

下面是我在客户端设置连接的方法

private HubConnection? hubConnection;

    protected override async Task OnInitializedAsync()
    {
        Balance = await userService.GetUserBalance();
        hubConnection = new HubConnectionBuilder()
            .WithUrl(navigationManager.ToAbsoluteUri($"/ws/payment"))
            .WithAutomaticReconnect()
            .Build();
        await hubConnection.StartAsync();
    }

我在互联网上搜索,并试图按照文档,但仍然没有运气:(

am46iovg

am46iovg1#

您是否设置了客户端发送令牌?

[Inject]
private AuthenticationStateProvider AuthenticationStateProvider { get; set; } = default!;

[Inject]
private IAccessTokenProvider AccessTokenProvider { get; set; } = default!;

protected override async Task OnInitializedAsync()
{
    hubConnection = new HubConnectionBuilder().WithUrl(
        url: new Uri(uriString: "https://localhost:7040/eventhub"), 
        SetConnectionOptions)
            .Build();

    hubConnection.On<EventMessage>(
        methodName: "ReceiveEventMessageAsync",
        ReceiveEventMessageAsync);

    await hubConnection.StartAsync();
}

private void SetConnectionOptions(HttpConnectionOptions options) =>
        options.AccessTokenProvider = async () => await GetAccessToken();

private async ValueTask<string> GetAccessToken()
{
    var accessTokenResult = await AccessTokenProvider.RequestAccessToken();
    accessTokenResult.TryGetToken(out var accessToken);
    return accessToken.Value;
}

相关问题