Accessing Webservices from Silverlight - Security Issue - silverlight

I am trying to get a simple WebClient call working from Silverlight to youtube. I lifted the code from the Microsoft Silverlight site.
Here is the code:
private void SearchTrack_Click(object sender, RoutedEventArgs e)
{
Uri serviceUri = new Uri("https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=sara+smile&key=AAAAAAAAAAAAAAA");
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(serviceUri);
}
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
Stream responseStream = e.Result;
// Continue working with responseStream here...
}
}
The call fails with the following error:
[System.Security.SecurityException] = {System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.
If I just hit the URL from my browser the JSON is returned without an issue.
I am running the application from VS/Cassini so I don't think it is the FIle system problem identified at http://blogs.msdn.com/b/silverlightws/archive/2008/03/30/some-tips-on-cross-domain-calls.aspx.
Any help would be greatly appreciated.
Barry

Here it's said, that it's not possible so easy to interract with google apis from silverlight because of corssdomain access restrictions. You need to add some intermidiate WCF-service, through which Your SL-app will communicate with youtube api.

Related

Sharepoint COM works from Silverlight but not from WPF

I'm running into an interesting situation. I need to access a SharePoint site asset library from both a WPF application and an Silverlight application. My Silverlight application is working 100%, but my WPF application gets a (500) Internal Server Error back from the service.
Silverlight Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
ClientContext clientContext = new ClientContext("http://myfullyQualifiedName.com");
clientContext.Load(clientContext.Web);
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
}
private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
{
}
private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
{
}
WPF Code:
private void Button_Click(object sender, RoutedEventArgs e)
{
ClientContext clientContext = new ClientContext("http://myfullyqualifiedname.com/");
//clientContext.Credentials = new NetworkCredential("UserName", "Password", "Domain");
clientContext.Load(clientContext.Web);
clientContext.ExecuteQuery();
}
I have tried with and without specifying credentials, either way I get the Internal server error.
Both Silverlight and non Silverlight Sharepoint client DLL's that I use has is version 14.4762.1000.
Now if I change the URL to one of our other sites, the WPF Code works flawlessly. So I think it must be a SharePoint settings somewhere.
Solved !! Why WPF Authentication wouldn't work when Silverlight works. (WPF was trying to use Kerberos, Silverlight was using NTLM) - Simple fix:
ClientContext _clientContext = new ClientContext(sharePointSiteUrl);
Web _web = _clientContext.Web;
_clientContext.Load(_web, website => website.Title);
_clientContext.Load(_web.Webs);
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(sharePointSiteUrl), "NTLM", CredentialCache.DefaultNetworkCredentials);
_clientContext.Credentials = cc;
_clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
_clientContext.ExecuteQuery();
ListCollection _listCollection = _web.Lists;

Silverlight calling WCF

I am new to silverlight, just wanted know if the steps involved in calling the WCF service same in Silverlight as it was in asp or is there any difference.If there is any difference then request you to help me out.
Thanks in advance.
The main things that you needs aware are
silverlight support only basichttpbinding
you need to have either clientaccesspolicy.xml file or crossdomain.xml in your root folder of wcf host server, then only you can successfully call a webservice from silerlight
http://msdn.microsoft.com/en-us/library/cc197955%28v=vs.95%29.aspx
I got the answer, there is slight difference between asp and silverlight.
In silverlight all service calls are asynchronous, so you have to have an eventhandler for when the async call is completed you get your data.
Just a small e.g
When you call your WCF service from asp, you use
proxy_http.FunctionClient fc = new proxy_http.FunctionClient();
txtDisplay.Text = fc.Add(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text)).ToString();
in case of Silverlight you will use
private void Add_Click(object sender, RoutedEventArgs e)
{
proxy_htt.FunctionClient fc = new proxy_htt.FunctionClient();
fc.AddCompleted += new EventHandler<proxy_htt.AddCompletedEventArgs>(fc_AddCompleted);
fc.AddAsync(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text));
}
void fc_AddCompleted(object sender, proxy_htt.AddCompletedEventArgs e)
{
txtResult.Text = e.Result.ToString();
}

Problem accessing localhost URI from Silverlight

(repost because of SO outage; apologies if the other one re-appears)
I'm building a Silverlight app that will run on Azure. My VS solution has two projects: the web role and the Silverlight. The web role has a Service that works. (I can go to localhost:88/expenseservice.svc/expenses and get the data I want.)
I am trying to access that data from Silverlight:
private void MainPage_Loaded(object sender, RoutedEventArgs args)
{
WebClient data = new WebClient();
data.DownloadStringCompleted += new DownloadStringCompletedEventHandler(data_DownloadStringCompleted);
Uri dataSource = new Uri("localhost:88/expenseservice.svc/expenses");
data.DownloadStringAsync(dataSource);
}
void data_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.InnerException.Message);
return;
}
// ...
}
However, this does not work. The message box shows the error:
The URI prefix is not recognized.
Here is the full exception:
e.Error.InnerException = {System.NotSupportedException: The URI prefix is not recognized.
at System.Net.WebRequest.Create(Uri requestUri)
at System.Net.WebClient.GetWebRequest(Uri address)
at System.Net.WebClient.DownloadStringAsync(Uri address, Object userToken)}
Is it complaining about localhost? Am I supposed to be doing something differently? Perhaps this is what "Add Service Reference" is for?
I think the prefix is not recognized because it is missing. The prefix should be the first part that describes what kind of service your URI is pointing to. For example http:// of svn:// and so on..
Just add the right one and it should work.. (I've never used Silverlight neither anything Microsoftish so I'm just guessing)

Silverlight Windows Phone 7: Load Images From URL

I got the code below that is trying to load an image from the web into an Image control, when I run it I get an error on the given line that no network access is allowed:
private void button1_Click(object sender, RoutedEventArgs e)
{
WebClient webClientImgDownloader = new WebClient();
webClientImgDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(webClientImgDownloader_OpenReadCompleted);
webClientImgDownloader.OpenReadAsync(new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute));
}
void webClientImgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(e.Result); // ERROR HERE!
image1.Source = bitmap;
}
Silverlight for Windows Phone 7
Trying to download content with WebClient will require a client access policy file to be present on the source server. For images you can avoid this requirement by doing it like this:-
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
}
I see you're retrieving the image from Dilbert.com does that site have a cross domain policy file?
Silverlight doesn't support GIF only JPG, so I wrote:
www.lenniedevilliers.net/displaygif.aspx?link=http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif
the displaygif.aspx page convert the GIF into a JPG.
Can you give us the full exception stack trace? the error could be that your phone emulator does not have internet access, or it could be the image on the dilbert server that does not allow anonymous requests that did not originate from their site ... so guidance on a solution will differ :-)

Download a html file in Silverlight

I'm trying to use the WebClient class to download a html file from another website and present it as a text stream, But I'm getting a security error, what am I doing wrong, or is this another one of Silverlights security "Features"
[code]
namespace ImageScrape
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
WebClient cl = new WebClient();
cl.OpenReadCompleted += new OpenReadCompletedEventHandler(cl_OpenReadCompleted);
cl.OpenReadAsync(new Uri(#"http://www.google.co.uk/",UriKind.Absolute));
}
void cl_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
testTextBlock.Text = e.Result.ToString();
}
}
}
[/code]
EDIT
Thanks guys, I was really hoping I wouldn't have to create this as a WCF service as 1) I only know the basics and 2) The idea is that you can use this .xap without having to connect to a central server, mainly because for this I don't have a server that I could host a WCF service on.
Does anyone know a way to get around this, or anywhere that would host a WCF service for free?
I think there are security issues with going directly to another site from the silverlight client.
The best work around for this would be to move this code into a web service and then serve the content you require to client from there.

Resources