3

I have an aspx-page in the Layouts-folder. A timejob makes a httpwebrequest on this page and the response-html is saved as a pdf. The httpwebrequest works fine, when its not called in a timerjob (ex. on another layouts-page), bbut when i execute the GetReponse()-Method in the timerjob i get an 401 (Unauthorized) error.

With this code it works in w3wp.exe

request.Method = "GET";
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultNetworkCredentials;

The only way i made it work in owstimer.exe is with

request.Credentials = new NetworkCredential(user, password, domain);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

But we don't whant to store the passwort somewhere in plaintext.

How can i make it work in owstimer.exe without providing the credentials?

4
  • Please elaborate: your want to avoid to have to provide the credential OR you want to avoid to store them in plain text? If the later suffice, I may have an idea.
    – SPArcheon
    Apr 3, 2013 at 12:44
  • if possible i want to avoid providing credentials, but if there isn't another way i'm open to your idea
    – domueni
    Apr 3, 2013 at 12:46
  • request.UseDefaultCredentials = true; should be sufficient, try it. Apr 3, 2013 at 13:02
  • Make sure that the code you're running is not making use of the SPContext or HttpContext as it wont exist in the timer job scope which might explain why the credentials need to be explicitly set.
    – Ralph W
    Apr 3, 2013 at 13:34

1 Answer 1

1

Disclaimer: While this won't bypass the need to provide a set of user credential, at least will somehow avoid to store them as clear text.

The general idea is to use the Secure Store Service to store a set of credential that will be used in the request. You need to define an "app" and then access that application via codebehind from the timer job instance.

This sample from msdn should give you a quick start on the implementation. I have actually done this to provide site collection creation from a SPLongOperation context.

(note: I will add some more info/screens - have some network problem now. In the meantime free to ask further info as required)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.