DomainService1是一个作为SOAP服务公开的RIA域服务。使用[RequiresAuthentication]和[RequiresRole(“xyz”)]属性来保护此服务。
在web.config中,已启用roleManager,并将身份验证模式设置为Forms。
测试客户端使用以下代码进行身份验证并调用远程服务操作:
var auth = new myAuth.AuthenticationDomainServiceSoapClient();
var svc = new mySvc.DomainService1SoapClient();
try
{
string myCookie;
using (new OperationContextScope(auth.InnerChannel))
{
var user = auth.Login(svcUser.Text, svcPass.Text, false, string.Empty);
var res = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name];
myCookie = res.Headers[HttpResponseHeader.SetCookie];
}
using (new OperationContextScope(svc.InnerChannel))
{
var octx = OperationContext.Current;
HttpRequestMessageProperty request = new HttpRequestMessageProperty();
request.Headers["Cookie"] = myCookie;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request;
var results = svc.GetItems();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我可以看到对auth.Login的调用实际上返回了正确的用户,在该对象中我可以看到角色设置正确。但是,对GetItems的调用失败,并且引发了“访问操作被拒绝”的异常。
我忽略了什么吗?你能看到我错过的任何明显的东西吗?
提前致谢,
干杯, 詹卢卡。
[编辑] 我想在EventLog中添加,我得到这个: 请求的表单身份验证失败。原因:提供的票证无效。
知道原因吗?
干杯。
答案 0 :(得分:0)
当我存储在cookie中的数据太长时,我遇到了类似的问题(例外)。尝试仅在会话cookie中存储重要数据,因为它限制为4k。即使登录成功,在后续调用中也会抛出访问被拒绝错误,因为cookie太大了。
答案 1 :(得分:0)
发布到此answer问题的RIA Authentication from a Web Services project似乎提供了缺失的链接。
您的代码(和我的代码)中缺少的额外步骤是用于读取HttpResponseHeader.SetCookie属性的FormatCookie()方法。
/// <summary>
/// Formats a request cookie string from the cookies received from the authentication service
/// </summary>
/// <param name="input">The cookie string received from the authentications service</param>
/// <returns>A formatted cookie string to send to data requests</returns>
private static string FormatCookie(string input)
{
string[] cookies = input.Split(new char[] { ',', ';' });
StringBuilder buffer = new StringBuilder(input.Length * 10);
foreach (string entry in cookies)
{
if (entry.IndexOf("=") > 0 && !entry.Trim().StartsWith("path") && !entry.Trim().StartsWith("expires"))
{
buffer.Append(entry).Append("; ");
}
}
if (buffer.Length > 0)
{
buffer.Remove(buffer.Length - 2, 2);
}
return buffer.ToString();
}
就我个人而言,我已经使用了这里描述的CookieManager技术http://blogs.msdn.com/b/davrous/archive/2010/12/03/how-to-open-a-wcf-ria-services-application-to-other-type-of-clients-the-soap-endpoint-3-5.aspx,然后将FormatCookie()方法添加到其中以使其正常工作。