SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

If a request to a certain site collection comes in from an anonymous user, I want to do a sign in as a specific user. For this I would like to use a HTTP module. So basically I want to do something like this:

private void ContextAuthenticateRequest(object sender, EventArgs e)
    if (HttpContext.Current.User == null)
        //Sign in here

How can I do the actual signing in?

share|improve this question
:) if you login as anonymous , how can you login as specific user ? i think you need to reframe question . – UserSP Jul 22 '14 at 14:46
I don't want to log in AS anonymous, I want to log in as a specific user if a request is made by a user that is not (yet) logged in. This would be the same user every time, think of it as a dedicated guest account or something. Obviously the details are simplified for this question as they are not relevant. What's important is: "How do I do a Sharepoint login via code?" and "It must work in the AuthenticateRequest handler of an HTTP module". – Hatch Jul 22 '14 at 15:32
check answer , this should give u a start – UserSP Jul 22 '14 at 20:12

PS :

class LoginModule {
public void Init(HttpApplication context)
 context.PreRequestHandlerExecute += new EventHandler(UglyHack);

void UglyHack(object sender, EventArgs e)
 HttpCookie wannabe = (HttpContext.Current.Request.Cookies["_sp_admin_wanna_be_user"]);
 if (wannabe != null && SPContext.Current.Web.CurrentUser.IsSiteAdmin)
     SPWeb cw = SPContext.Current.Web;
     typeof(SPWeb).GetField("m_CurrentUser", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(
share|improve this answer
This doesn't work for my situation (still no user signed in after code has run) and it would indeed be a really ugly hack if it did - setting the current user using reflection. – Hatch Jul 23 '14 at 8:07

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.