Silverlight 4 Web客户端身份验证-有人可以使用它吗?
因此,新的 Silverlight 4 Beta 最好的部分之一是他们最终实现了网络堆栈中缺失的重要功能 - 网络凭据!
在下面,我有一个有效的请求设置,但由于某种原因,当请求返回时我收到“安全错误” - 这是因为 twitter.com 拒绝了我的 api 调用还是我在代码中丢失了某些内容?
最好指出的是,当我看到这段代码通过 fiddler 执行时,它显示跨域的 xml 文件已成功下拉,但这是 fiddler 显示的最后一个请求......
public void RequestTimelineFromTwitterAPI()
{
WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.ClientHttp);
WebClient myService = new WebClient();
myService.AllowReadStreamBuffering = true;
myService.UseDefaultCredentials = false;
myService.Credentials = new NetworkCredential("username", "password");
myService.UseDefaultCredentials = false;
myService.OpenReadCompleted += new OpenReadCompletedEventHandler(TimelineRequestCompleted);
myService.OpenReadAsync(new Uri("https://twitter.com/statuses/friends_timeline.xml"));
}
public void TimelineRequestCompleted(object sender, System.Net.OpenReadCompletedEventArgs e)
{
//anytime I query for e.Result I get a security error
}
So one of the best parts about the new Silverlight 4 beta is that they finally implemented the big missing feature of the networking stack - Network Credentials!
In the below I have a working request setup, but for some reason I get a "security error" when the request comes back - is this because twitter.com rejected my api call or something that I'm missing in code?
It might be good to point out that when I watch this code execute via fiddler it shows that the xml file for cross domain is pulled down successfully, but that is the last request shown by fiddler ...
public void RequestTimelineFromTwitterAPI()
{
WebRequest.RegisterPrefix("https://", System.Net.Browser.WebRequestCreator.ClientHttp);
WebClient myService = new WebClient();
myService.AllowReadStreamBuffering = true;
myService.UseDefaultCredentials = false;
myService.Credentials = new NetworkCredential("username", "password");
myService.UseDefaultCredentials = false;
myService.OpenReadCompleted += new OpenReadCompletedEventHandler(TimelineRequestCompleted);
myService.OpenReadAsync(new Uri("https://twitter.com/statuses/friends_timeline.xml"));
}
public void TimelineRequestCompleted(object sender, System.Net.OpenReadCompletedEventArgs e)
{
//anytime I query for e.Result I get a security error
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现有 2 个问题导致此请求引发安全异常
1) - 在 这个视频,作者:Tim Heuer 事实证明我的 VS2010 w/ Silverlight 4 工具包安装与最终版本不匹配,所以我错过了显示的选项“浏览器外设置”对话框,提供“在浏览器外部运行时需要提升信任”复选框。
在上面列出的视频中,Tim 检查了这一点,以便 Silverlight 应用程序可以与 twitter API 对话,
但因为我的应用程序没有此选项,所以我必须手动编辑 xml 文件,使其如下所示。您可以在项目文件夹的属性下或直接在 Visual Studio 中找到此 xml。
请注意 **安全设置 ElevatedPermissions="Required"
保存此设置后,相当于像蒂姆在视频中那样检查此设置。
2) - 当我观看 Tim 的视频时,我注意到你必须在浏览器之外对其进行调试才能使其正常工作。因此,安装该应用程序并在浏览器外运行它。这个应用程序现在可以运行了。
我将写一篇简短的博客文章来总结我在测试版中使用网络堆栈的经验,并为感兴趣的人提供链接。
更新
我终于写了一篇博客帖子关于我使用 Silverlight 4 构建浏览器外 Twitter 客户端的经验(如果有人感兴趣的话)。
I found 2 issues that caused this request to throw the security exception
1) - In this video by Tim Heuer it turns out my VS2010 w/ Silverlight 4 toolkit installation didn't match the final build so I'm missing the option that shows up in the "out of browser settings" dialog that provides the checkbox for "Require elevated trust when running outside the browser".
In the video listed above Tim checks this so the Silverlight app can talk to the twitter API
But because my application didn't have this option I had to manually edit the xml file so it looked like the below. You can find this xml under properties in the project folder or inside visual studio directly.
Notice the **security settings ElevatedPermissions="Required"
After you save this it's equivalent to checking this as Tim did in the video.
2) - as I was watching that video by Tim I noticed that you have to debug this outside of the browser to get it working. So install the app and run it outside the browser. This app now works.
I'll write a short blog post to summarize my experience with the networking stack under the beta and link to it for anyone interested.
Update
I finally wrote a blog post about my experience building an out of browser twitter client using Silverlight 4 if anyone is interested.