【问题标题】:Dataservice authentication not working with serviceContext.GetReadStreamUri(..)数据服务身份验证不适用于 serviceContext.GetReadStreamUri(..)
【发布时间】:2011-08-10 23:08:50
【问题描述】:

我有一个 Odata 服务和一个 WPF 客户端应用程序。 一些 Odata 服务实体附有图像(即客户端)。

只要我不应用身份验证,流式传输就可以工作。我可以查看和更改图像。一旦我强制执行身份验证,一切都会按预期工作,给定凭据检查。除了图像之外的所有内容。以下是相关的代码步骤/截图。

窗口构造函数代码

bool iv = System.Web.Security.Membership.ValidateUser("userName", "pass");
ManageService = new InventoryContext(new Uri(...));
ManageService.SendingRequest += new EventHandler<SendingRequestEventArgs (ManageService_SendingRequest);

ManageService_SendingRequest 代码

//attach the authentication cookie to the request header
((HttpWebRequest)e.Request).CookieContainer = ((ClientFormsIdentity)Thread.CurrentPrincipal.Identity).AuthenticationCookies;

获取数据的调用是使用后台工作线程异步进行的 查询方法e()

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(FetchClient);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FetchClientsCompleted);
worker.RunWorkerAsync(ClientUUID);

FetchClient

var query = from o in ManageService.Clients where o.ClientUUID.Equals((Guid)e.Argument)
...
e.Result = query;

FetchClients 已完成

var res = e.Result as DataServiceCollection<Client>;
DataContext = res[0];  //this is all working, with and without authentication
//the next line, binding the stream to the image throws 'unauthenticated'
//it works well if authentication is disabled 
imgClient.Source = new BitmapImage(ManageService.GetReadStreamUri(DataContext));

如果我调试,通常与任何查询请求一起调用的 SendingRequest 方法不会触发调用 GetReadStreamUri(...)。 这就是我卡住的地方,如何对服务进行身份验证以获取流?

另外,我将 ManageService.GetReadStreamUri(DataContext) 生成的 URI 传递到浏览器中,它可以工作,如果登录,图像会显示在浏览器中。

有人有什么想法吗?

【问题讨论】:

    标签: wpf authentication odata wcf-data-services


    【解决方案1】:

    SendingRequest 处理程序只会触发 DataServiceContext 类(您的 ManageService)发送的请求。但在图片的情况下,您只能从 DataServiceContext 中获取 URL,然后让 BitmapImage 实际向该 URL 发出 HTTP 请求。因此,该事件不会针对该请求触发。我不知道 BitmapImage 是否有办法让您连接到 HTTP 请求管道(我认为没有)。 您可以自己发出该请求,然后使用响应流作为位图图像的输入,在这种情况下,您可以完全控制请求,从而可以适当地实施身份验证。

    【讨论】:

    • Thanx @Vitek,我理解第一部分,我不知道你自己发出请求是什么意思。你的意思是我应该使用 GetReadStream() 而不是 GetREadStreamUri()?
    • 或者您可以使用例如 HttpWebRequest 类和从 GetReadStreamUri 返回的 URI 自己检索图像(不使用 DataServiceContext 为您获取图像)。
    • 好吧,我改变了我的 co 以使用 GetReadStream() 并且它现在可以正常工作,正如预期的那样。再次感谢您的指点。
    • 只是为了结束这个,这就是我最终使用 DataServiceStreamResponse resStream = ManageService.GetReadStream(DataContext); BitmapImage imageFromStream = new BitmapImage(); BimageFromStream.BeginInit(); imageFromStream.StreamSource = resStream.Stream; imageFromStream.CacheOption = BitmapCacheOption.OnLoad; imageFromStream.EndInit(); imgClient.Source = imageFromStream;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多