【发布时间】:2011-11-10 03:00:10
【问题描述】:
我在服务器端有一个 WCF 4.0 REST 服务(托管在 IIS 中)和一个 Android 客户端。 Android 客户端在自定义 HTTP 标头中发送加密的安全令牌,以便对用户进行身份验证。我已经实现了一个自定义ServiceAuthorizationManager,它从标头中提取安全令牌。令牌包含我可以从令牌中读取的用户名:
public class MyAuthorizationManager : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
var requestMessage = operationContext.RequestContext.RequestMessage;
var requestProperty = (HttpRequestMessageProperty)requestMessage
.Properties[HttpRequestMessageProperty.Name];
var token = requestProperty.Headers["X-MyCustomHeader"];
if (!string.IsNullOrEmpty(token))
{
var userName = GetUserNameFromToken(token);
if (!string.IsNullOrEmpty(userName))
{
// How to save userName now so that I can
// retrieve it in the service operations?
return true;
}
}
return false;
}
}
现在,我的问题是我在各种服务操作中还需要经过身份验证的用户的名称(主要是访问用户配置文件数据),我打算这样检索它:
public void MyServiceOperation()
{
string userName = OperationContext.Current
.ServiceSecurityContext.PrimaryIdentity.Name;
// check profile store for that userName and do something depending on
// profile settings
}
如何在CheckAccessCore 中设置此用户名?
像这样非常幼稚的试验......
operationContext.ServiceSecurityContext.PrimaryIdentity.Name = userName;
...不起作用,因为PrimaryIdentity.Name 是只读的。我认为需要更复杂的代码。
【问题讨论】:
标签: c# .net wcf wcf-security