我正在试图弄清楚如何使用ServiceStack的OnAuthenticated
函数将刚认证的用户订阅到SSE频道。
这是我的实际代码:
public override void OnAuthenticated(IRequest httpReq, IAuthSession session, IServiceBase authService, IAuthTokens tokens, Dictionary<string, string> authInfo) {
string subscriptionId = // ???;
string[] channels = { "mychan1", "mychan2" };
ServerEvents.SubscribeToChannels(subscriptionId, channels);
}
我的问题是:我如何将subscriptionId
绑定到刚认证的用户,以便为他/她订阅频道?
非常感谢!
答案 0 :(得分:0)
您无法代表会话中的用户订阅,客户端需要使用same Service Client they've authenticated with进行自己经过身份验证的服务器事件连接,例如:
初始化ServerEventsClient
:
var client = new ServerEventsClient(baseUri, channel=channelName) {
// Register any handlers...
};
使用内置服务客户端进行身份验证:
client.ServiceClient.Post(new Authenticate {
provider = "credentials",
UserName = "user",
Password = "pass",
RememberMe = true,
});
启动ServerEventsClient
以建立Authenticated Server Events连接:
client.Start();
或者see this previous answer,用于与JWT等不同的Auth Providers建立经过身份验证的服务器事件连接的示例。