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?

  • :) if you login as anonymous , how can you login as specific user ? i think you need to reframe question . – Nikhil J 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 – Nikhil J Jul 22 '14 at 20:12

PS : https://serverfault.com/questions/17782/logging-in-as-another-user-in-sharepoint/17979#17979

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(
|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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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