【问题标题】:authentication issues in RIA Domain Service (Access to operation denied)RIA 域服务中的身份验证问题(拒绝访问操作)
【发布时间】:2011-04-09 09:32:57
【问题描述】:

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

知道原因吗?

干杯。

【问题讨论】:

    标签: c# silverlight wcf ria


    【解决方案1】:

    当我存储在 cookie 中的数据太长时,我遇到了类似的问题(异常)。尝试仅在会话 cookie 中存储重要数据,因为它被限制为 4k。即使登录成功,在后续调用中也会抛出拒绝访问错误,因为 cookie 太大。

    【讨论】:

      【解决方案2】:

      发布到此RIA Authentication from a Web Services project 问题的answer 似乎提供了缺失的链接。

      您的代码(和我的)缺少的额外步骤是用于读取 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();
      }
      

      我个人使用了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 此处描述的 CookieManager 技术,然后将 FormatCookie() 方法添加到它以使其正常工作。

      【讨论】:

        猜你喜欢
        • 2012-06-16
        • 2016-03-15
        • 2019-12-12
        • 2018-08-18
        • 1970-01-01
        • 2017-11-28
        • 1970-01-01
        • 1970-01-01
        • 2018-01-06
        相关资源
        最近更新 更多