RIA域服务中的身份验证问题(访问操作被拒绝)



DomainService1是作为SOAP服务公开的RIA域服务。此服务是通过使用[RequiresAuthentication]和[RequiresRole("xyz")]属性来保护的。

web。配置已启用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中添加,我得到这个:请求的表单身份验证失败。原因:提供的机票无效。

知道原因吗?

欢呼。

当我存储在cookie中的数据太长时,我有一个类似的问题(异常)。尽量只在会话cookie中存储重要的数据,因为它被限制为4k。即使登录成功,在随后的调用中,它抛出访问被拒绝错误,因为cookie太大。

发布到这个Web服务项目问题的RIA身份验证的答案似乎提供了缺失的链接。

你的代码(和我的)缺少的额外步骤是用于读取HttpResponseHeader的FormatCookie()方法。SetCookie财产。

/// <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()方法使其工作

最新更新