locked
How to redirect user specific site/subsite? RRS feed

  • Question

  • Hello

    I am using ADAM based FORM based authentication. We are having 100+ users and users are created in group depending on the subsite.

    I want to creata logic that whenever user logged in thru login.aspx, they should automatically redirect to their assigned subsite/site, becuase we have not allowed user to access portal default poage.

    How can I manage this?

    Avi

    Wednesday, September 9, 2009 11:45 AM

Answers

  • Hello Avi

    I tried following code, and it is working nicely for me. Please test and let me know it works for you or not?

    FormsAuthentication.RedirectFromLoginPage(txtUserName.Text.Trim(), false); 
    string strUserUrl = "";
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        SPSite oSite = SPContext.Current.Site;
        SPWebCollection collWebs = oSite.OpenWeb().GetSubwebsForCurrentUser();
        foreach (SPWeb oWebsite in collWebs)
        {
            SPUserCollection collUsers = oWebsite.AllUsers;
            foreach (SPUser oUser in collUsers)
            {
                if (oUser.LoginName.ToUpper() == txtUserName.Text.Trim().ToUpper())             
    	    {                
                    strUserUrl = oWebsite.ServerRelativeUrl.ToString();
                }
            }
    
            oWebsite.Dispose();
        }
    });
    
    Response.Redirect("http://portal/"+strUserUrl,true);
    
    • Marked as answer by Avian123 Tuesday, September 15, 2009 3:57 PM
    Tuesday, September 15, 2009 2:39 PM

All replies

  • Hi,

    You can use the PostAuthenticateRequest event handler to achieve this. The event fires after you have authenticated to the site through the Formsauthentication Module.

    If you are using your an asp.net login control(which sharepoint uses by default in the forms Login page) you can use some of the Events like LoggingIn,etc...

    For using the PostAuthenticateRequest you have to either write the code in the default global.asax or you have to have a class which inherits from
    Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication and change the global.asax to use your class

    More on the PostAuthenticateRequest:

    Microsoft.SharePoint.ApplicationRuntime.SPHttpApplication

    Regards,
    Madhu
    Wednesday, September 9, 2009 12:05 PM
  • Can you please suggest any example or article?
    Wednesday, September 9, 2009 12:59 PM
  • Avi

    You can use following code also. In this code you can get the site list of logged in user and then then you can redirect to the specific site. Let me know how it goes?

    SPSite oSite = SPContext.Current.Site;
                SPWebCollection collWebs = oSite.OpenWeb().GetSubwebsForCurrentUser();
                string strUserUrl = "";
                foreach (SPWeb oWebsite in collWebs)
                {
                    SPUserCollection collUsers = oWebsite.AllUsers;
                    foreach (SPUser oUser in collUsers)
                    {
                        if (oUser.LoginName.ToUpper() == username.ToUpper()) // username is your logged in name username
                        {
    
                            strUserUrl = oWebsite.ServerRelativeUrl.ToString() '
                        }
                    }
                    oWebsite.Dispose();
                }
    Wednesday, September 9, 2009 1:56 PM
  • Hi,

    You can see this post for an Example

    http://blogs.sqlxml.org/bryantlikes/articles/583.aspx

    He uses the authenticateRequest.

    if you do not want to use the global.asax, you can create a custom HttpModule for the purpose as described in this post

    http://blogs.msdn.com/jannemattila/archive/2007/06/25/adding-users-to-sharepoint-dynamically-at-first-request-after-authentication.aspx

    Regards,

    Madhu
    • Proposed as answer by Madhu918 Thursday, September 10, 2009 1:29 PM
    Wednesday, September 9, 2009 2:55 PM
  • Hello Avi

    I tried following code, and it is working nicely for me. Please test and let me know it works for you or not?

    FormsAuthentication.RedirectFromLoginPage(txtUserName.Text.Trim(), false); 
    string strUserUrl = "";
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        SPSite oSite = SPContext.Current.Site;
        SPWebCollection collWebs = oSite.OpenWeb().GetSubwebsForCurrentUser();
        foreach (SPWeb oWebsite in collWebs)
        {
            SPUserCollection collUsers = oWebsite.AllUsers;
            foreach (SPUser oUser in collUsers)
            {
                if (oUser.LoginName.ToUpper() == txtUserName.Text.Trim().ToUpper())             
    	    {                
                    strUserUrl = oWebsite.ServerRelativeUrl.ToString();
                }
            }
    
            oWebsite.Dispose();
        }
    });
    
    Response.Redirect("http://portal/"+strUserUrl,true);
    
    • Marked as answer by Avian123 Tuesday, September 15, 2009 3:57 PM
    Tuesday, September 15, 2009 2:39 PM
  • Thanks Ashish, this is what I am looking for.

    Tuesday, September 15, 2009 3:58 PM