0

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?

1 Answer 1

1

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
            Console.ReadLine();

        }
    }
}

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

Your Answer

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

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