有没有办法知道哪个用户正在调用服务器端的WCF Ria服务?客户端是siverlight,用户必须先通过身份验证才能使用系统。
我需要知道哪个用户实际上在调用我当前任务中的服务,谢谢,我搜索了很多,但似乎没有好的发现。
发布于 2011-12-29 12:45:23
一旦客户端成功清除了您的身份验证挑战,服务器就可以向客户端上的调用者发出令牌。在对服务器的后续调用中,客户端将发送令牌作为参数之一,服务器将验证令牌并做出相应响应。
令牌可以包含标识给定用户的一段信息,实现这一点将提供您正在寻找的功能。
生成令牌的唯一原则是它们是唯一的、不可预测的和可过期的。我总是加密我的令牌,使它们看起来像胡言乱语,但这一步是可选的。
发布于 2011-12-29 20:10:07
我也做了很多“谷歌搜索”,在得到解决方案之前有很多令人头疼的事情。我不使用RIA-Services -但它应该(希望)是相同的...:
SL-Client向服务器发送“登录请求”。
在(WCF)服务器端,我执行以下操作(LoginData =SL-Client的Return-Info ):
public LoginData LoginRequest() {
(...)
OperationContext context = OperationContext.Current;
System.ServiceModel.Channels.MessageProperties prp = context.IncomingMessageProperties;
System.ServiceModel.Channels.RemoteEndpointMessageProperty endPrp = prp[System.ServiceModel.Channels.RemoteEndpointMessageProperty.Name] as System.ServiceModel.Channels.RemoteEndpointMessageProperty;
(...)
try {
clientIP = endPrp.Address;
System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse(clientIP);
System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(ipAddress);
(...)如果您想检查用户的WindowsPrincipal,您可以执行以下操作(securityGroup =服务器端设置,哪些用户可以登录):
(...)
switch (securityGroup) {
case SecurityGroup.AllClientsCanAccess: {
clientCanLogin = true;
} break;
case SecurityGroup.UseWindowsCredentials: {
if (OperationContext.Current.ServiceSecurityContext != null && OperationContext.Current.ServiceSecurityContext.WindowsIdentity != null) {
if (OperationContext.Current.ServiceSecurityContext.WindowsIdentity.IsAuthenticated) {
if (subSecurityInfo1 == true) { // only clients in specific roles can log in
bool userRoleFound = false;
WindowsPrincipal userPrincipal = new WindowsPrincipal(OperationContext.Current.ServiceSecurityContext.WindowsIdentity);
if (userPrincipal == null)
break;
foreach (string userRoleToPass in subSecurityList) { // subSecurityList = settings, which roles can pass
loginError.ErrorInfo += string.Format("{0}\n", userRoleToPass);
if (userPrincipal.IsInRole(userRoleToPass)) {
clientCanLogin = userRoleFound = true;
break;
}
}
if (userRoleFound) {
loginError.ErrorInfo = string.Empty;
break;
}
else {
loginError.ErrorNo = LoginErrorCodeNoEnum.UserIsNotInRole;
}
}
(...)希望这能帮上忙。
https://stackoverflow.com/questions/8663524
复制相似问题