Interacting with google appengine from C# - c#

I'm writing a google appengine application, which stores data and has a web front end. I want to be ableto pull down this data in a C# program. This means I need to authenticate with the site (users must be logged in to view the data). How can I authenticate like this? I tried setting credentials on the WebClient but I keep getting the google login page.
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
//should it be username#gmail.com ??
client.BaseAddress = "http://nosoperor-internal.appspot.com";
String s = client.DownloadString("/bank");
//s now contains the google login page, unfortunately

I found an answer in this blog:
Accessing authenticated Google App Engine services from a .NET CF client
and it works ^^

AFAIK, you need to probably use Google Data protocols and account APIs to authenticate.
Edit: I would download the .Net client library and try the examples in there. This is a copy paste from the docs:
Service service = new Service("cl", "exampleCo-exampleApp-1"));
// Set your credentials:
service.setUserCredentials("jo#gmail.com", "mypassword");

Related

Windows Authentication in ASP .Net for WebApi

I have written a simple ASP.Net WebAPI that download a file from an on-prem tfs server using a rest call. The issue is when I connect to this site from my user account(corp domain), and set the credentials in webRequest to CredentialsCache.DefaultCredentials, it works fine since I am a vlid user on the tfs project.
Now someone else in my team wishes to use the same webAPI and when they hit the same url, I could see that its my credentials being used again to download the file.
I suspect this is because of using CredentialsCache.DefaultCredentials in my controller code, but I am not able to find a way using which I should be able to use the client's identity to be used for the download request.
My question is , is there a way to extract the windows identity or network credentials of the remote user(on the same domain) which I can use for Authentication. I am sorry if this is a DUP but I could not find a targetted answer to my question yet. Sample code is pasted below.
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
var webResponse = request.GetResponse();
var webStream = webResponse.GetResponseStream();
if (webStream != null)
{
var objReader = new StreamReader(webStream);
return objReader.ReadToEnd()
}
}
I dont know if I understand you correctly but I think impersonation is what you're looking for:
http://msdn.microsoft.com/en-us/library/xh507fc5.ASPX

Downloading file from secure sharepoint server c#

I'm trying to download a file from a Sharepoint server, however my code to supply credentials. Its security is the sort that you get a little WinForms pop up asking for login details like that of a network drive as opposed to a logon screen.
This is what I am using
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("william.dunne", "legacy01");
client.DownloadFile(URL, Filename);
}
It seems you are missing the domain part of your username. Try supplying the domain to NetworkCredentials overloaded constructor.

WebClient Credentials SharePoint Intranet

I am working on a problem right now and it involves downloading multiple PDF files to our DMZ from our LAN SharePoint server.
What we are trying to accomplish is a "NewsFeed" type application. What it does is take list data from uploaded Content Types made in SharePoint, then post them to our external website hosted on the DMZ, with a link to the appropriate pdf file. We used the REST API that sharepoint has to accomplish the text layout fairly easily, but the problem we are now running into is copying the .pdf of the uploaded document from the SharePoint site, onto the DMZ, so we can store them in a cache, and the user can access them through an href.
Here is code that works, to access the SharePoint REST API.
// Create the connection to sharepoint. Credentials are managed within web.config
SharePointListsREST.SharePointDataContext dc = new SharePointListsREST.SharePointDataContext(new Uri(ConfigurationManager.AppSettings["spUrl"]));
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTLM", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
dc.Credentials = cc;
This works perfectly, it is able to authenticate, grab the data and display it. For the PDF download functionality we have this.
private void GetPDF(string URL, string path)
{
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
WebClient wc = new WebClient();
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTML", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
wc.Credentials = cc;
wc.DownloadFile(URL, path);
}
which does not work at all and throws a 401 Not Authorized error.However, if you go to try to access the pdf file by going to http://XX.XX.XX.XX/.../test.pdf you will be prompted for a username/password. Entering my windows based credentials will give me access to the pdf.
I have tried using wc.UseDefaultCredentials = true; but it does not work either. Does anyone have any ideas on how I can get this to work with the webclient?
Here is one of the error logs from IIS.
2012-07-11 00:56:12 XX.XX.XX.XX GET /Employee+Images/_t/sample.jpg - 80 - 192.168.200.12 - 401 2 5 0
Impersonating the user and running whole WebClient code inside impersonated block is an option when you use Windows auth on SharePoint site.
See this question How can I use Directory.CreateDirectory with a different user id? on impersonating a user.
Or you simply can run whole process as that special user with Runas - no code changes would be needed.

How to get Google Analytics data using OAuth?

Hy guys, we are developing a system which will provide users with access to Google Analytics. I'm trying to implement it in the way so user don't need to enter their Google login credentials on our site, so trying to get it work using their login.
I have a solution which gets analytics using user's email and password. I'm looking for a solution which will not require user's email and password but can not find anything.
How can it be done? any advices or links will be appreciated.
thanks
Ok, guys, after a few days of struggle I finally figured this out. There is no documentation on the Internet and people who had done it before did not want to share their success by some reason. I found this discussion which helped me.
To make it work you will need DotNetOpenAuth from http://www.dotnetopenauth.net/ and gdata from http://code.google.com/p/google-gdata/
so
using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth;
using Google.GData.Client;
using Google.GData.Analytics;
In DotNetOpenAuth there is sample project named OAuthConsumer which you need.
Change it to requiest authorization for Analytics:
GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Analytics);
This will get you Token and Token secret.
You can use them like this:
GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("cp", TokenManager.ConsumerKey); //ConsumerKey actually is the name of web application
requestFactory.ConsumerKey = TokenManager.ConsumerKey;
requestFactory.ConsumerSecret = TokenManager.ConsumerSecret;
requestFactory.Token = AccessToken;
requestFactory.TokenSecret = TokenManager.GetTokenSecret(AccessToken);
requestFactory.UseSSL = true;
AnalyticsService service = new AnalyticsService(requestFactory.ApplicationName); // acually the same as ConsumerKey
service.RequestFactory = requestFactory;
const string dataFeedUrl = "https://www.google.com/analytics/feeds/data";
DataQuery query1 = new DataQuery(dataFeedUrl);
This service you can use like here or here
And the last thing, you WILL NOT be available to try and test it on localhost so you will need a domain which MUST be registered with Google here in order to get consumer key and secret
There's a .NET/C# class for Google Data authentication that can be used to access the Google Analytics Data Export API (since the API is part of the Google Data standard, though you might need to make Google Analytics specific adjustments.)*
The authentication is best managed by creating a Google Registered Application, as this allows you to make the authentication without security warnings (and, for that matter, security lapses).
There are three forms of supported authentication; the 'secure'/passwordless ones are OAuth and AuthSub (which is the Google-proprietary version of OAuth); the hardcoded username and password version is referred to by Google as 'ClientLogin', and is not considered secure or ideal for multiple-user applications.
*(Since you tagged the question .netc#)
Edit: More details on using AuthSub or OAuth with the .NET library:
AuthSubSupport: http://code.google.com/p/google-gdata/wiki/AuthSubSupport
Code Samples on how to use the libraries for OAuth authentication: http://code.google.com/apis/gdata/docs/auth/oauth.html#2LeggedOAuth (Click the .NET tab).
Basics of working with OAuth are here: http://code.google.com/apis/accounts/docs/OpenID.html#working
Authenticating with OAuth: http://code.google.com/apis/accounts/docs/OAuth.html
After you authenticate a user with OAuth, you will have the request token that works like the one you get back from Google's login API. From there, it should be the same as username/password.
I don't think you need to mess with OAuth.
The google analytics api lets you pass credentials. Just start from this data feed example.
http://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/Analytics_DataFeed_Sample/dataFeed.cs
// Configure GA API and do client login Authorization.
AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");
asv.setUserCredentials(clientUser, clientPass);
Download the client library here
http://code.google.com/apis/analytics/docs/gdata/gdataLibraries.html
To get a feel for data queries, play with this and then copy the values into the example above
http://code.google.com/apis/analytics/docs/gdata/gdataExplorer.html

how to use the windows login credentials for proxy authentication using C#

Is it possbile to use the windows login credential for proxy authentication using C#.
I have a facebook application, which calls the facebook methods. During every facebook call, it gives an error "407: proxy authentication required"
The following code will allow the user to set the proxy :-
WebProxy oWebProxy = new System.Net.WebProxy(ProxyServer, ProxyPort);
oWebProxy.Credentials = new NetworkCredential(ProxyUser,ProxyPassword,ProxyDomain);
oserv.Proxy = oWebProxy;
oserv.Credentials = new NetworkCredential(theusername, thepassword);
But, is there any other way of doing the same thing without hardcoding my company's login credentials.
You can use :
oWebProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
If it still does not solve your problem, then please refer to the site : http://support.microsoft.com/kb/813834
Try creating a WebProxy with a default constructor (don't supply the proxy server) and see if it works for you..
WebProxy oWebProxy = new System.Net.WebProxy();
The default constructor will take the credentials from the system as you entered them in your web browser..

Categories

Resources