Dataservice身份验证无法使用serviceContext.GetReadStreamUri(..)

时间:2011-08-09 17:46:11

标签: wpf authentication odata wcf-data-services

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

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

Window构造函数代码

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;

使用后台工作程序获取数据的调用是异步的 查询Methode()

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;

FetchClientsCompleted

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,将其传递到浏览器并运行,如果已登录,图像将显示在浏览器中。

任何想法?

1 个答案:

答案 0 :(得分:1)

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