Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 . –  Nikhil Jul 22 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 at 15:32
    
check answer , this should give u a start –  Nikhil Jul 22 at 20:12

1 Answer 1

PS : http://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(
                    SPContext.Current.Web,
                    cw.AllUsers[wannabe.Value]);
 }
 }
 }
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 at 8:07

Your Answer

 
discard

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.