SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to run a timer job in SharePoint 2010 that ultimately will call into the BCS to retrieve info from an Oracle database. My timer service is running as a domain user, so I expected this (when running through the timer job)

var timer_identity = System.Threading.Thread.CurrentPrincipal.Identity;

to represent the WindowsIdentity of that domain user. Instead, it's a GenericIdentity with no name, and as a result my attempts to access the database through BCS and the secure store service end up failing.

Why is my identity not what I think it should be? And what's the best way to get this code to run as a specific domain-based service account?

share|improve this question

You don't have any running context in timer jobs so you have to set the identity yourself.

var timer_identity_user = new WindowsPrincipal(WindowsIdentity.GetCurrent());
System.Threading.Thread.CurrentPrincipal = timer_identity_user;

Here is a quick console program to demonstrate

using System;
using System.Security.Principal;

namespace TestIdentity
    class Program
        static void Main(string[] args)
            var ti = System.Threading.Thread.CurrentPrincipal.Identity; 
            Console.WriteLine("ti name = {0}",ti.Name); // blank
            var tiw = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            System.Threading.Thread.CurrentPrincipal = tiw;
            var tiNext = System.Threading.Thread.CurrentPrincipal.Identity;
            Console.WriteLine("tiNext name = {0}", tiNext.Name); // Correct


Info on WindowsPrincipal and IPrincipal if you want to use a specific account.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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