ServiceStack服务OnUnsubscribe \ OnSubscribe \ OnConnect用户DisplayName错误

时间:2016-02-16 23:30:58

标签: c# .net servicestack httpserver

在我的服务堆栈中,我有以下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

1 个答案:

答案 0 :(得分:2)

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

您可以使用ServerEventsFeature.OnCreated()自定义挂钩更改返回到Server Events客户端的自定义元数据,例如您可以更改displayName返回的地址:

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