ServiceStack Service OnUnsubscribe\OnSubscribe\OnConnect 用户 DisplayName 错误

ServiceStack Service OnUnsubscribe\OnSubscribe\OnConnect user DisplayName is wrong

在我的服务堆栈中,我有以下 AuthRepository 初始化:

var userRep = new InMemoryAuthRepository();
container.Register<IUserAuthRepository>(userRep);

string hash;
string salt;
var pwd = "ValidPassword";

new SaltedHash().GetHashAndSaltString(pwd, out hash, out salt);
userRep.CreateUserAuth(new UserAuth
{
    Id = 1,
    DisplayName = "CustomDisplayName",
    Email = "test@gmail.com",
    UserName = "CustomUserName",
    FirstName = "FirstName",
    LastName = "LastName",
    PasswordHash = hash,
    Salt = salt,
}, pwd);

在同一服务中我有一些事件处理程序:

private void OnUnsubscribe(IEventSubscription obj)
{
    Console.WriteLine(obj.DisplayName + " has unsubbed\n");
}

private void OnSubscribe(IEventSubscription obj)
{
    Console.WriteLine(obj.DisplayName + " has subbed\n");
}

private void OnConnect(IEventSubscription obj, Dictionary<string, string> dict)
{
    Console.WriteLine(obj.DisplayName + " has connected\n");
}

问题是我的 obj.DisplayName 是 CustomUserName 而不是 CustomDisplayName(并且 obj.UserName 也是 CustomUserName)。这是它应该如何工作吗?如果是 - 如何更改 DisplayName?目前我使用默认 CredentialsAuthProvider

对于经过身份验证的用户,ServerEvents feature 使用 session.UserName(如果它存在,因为它是唯一的),如果没有定义,则返回给用户 DisplayName

您可以使用 ServerEventsFeature.OnCreated() 自定义挂钩来更改返回给服务器事件客户端的自定义元数据,例如您可以更改返回的 displayName

Plugins.Add(new ServerEventsFeature
{
    OnCreated = (sub, req) => {
        sub.Meta["displayName"] = req.GetSession().DisplayName;
    }
});