kamagra how much to take

Redirect Users SharePoint WebPart

This code snippet will allow you to redirect users to various sites based on their login.

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. using Microsoft.SharePoint.Portal.UserProfiles;
  6.  
  7. using Microsoft.SharePoint.WebPartPages;
  8. using Microsoft.SharePoint.Portal;
  9. using Microsoft.SharePoint;                         // Needed for Site Objects (SPSite)
  10. using Microsoft.SharePoint.WebControls;               // Needed for SPControl
  11. using System.Security.Principal;
  12. using System.Runtime.InteropServices;
  13.  
  14.  
  15.  
  16. namespace ARB.PageRedirect
  17. {
  18.         //==============================================================================================
  19.         /// <summary>
  20.         /// Adam Buenz
  21.         /// ARB Security Solutions
  22.         /// </summary>
  23.         //==============================================================================================
  24.         //
  25.         [DefaultProperty(""), ToolboxData("<{0}:PageRedirector runat=server></{0}:PageRedirector>")]
  26.         public class PageRedirector : System.Web.UI.WebControls.WebControl
  27.         {
  28.                 //
  29.                 //------------------------------------------------------------------------------------------
  30.                 /// <remarks>PortalOrSite Property: Portal or Site indicator ("P" or "S")</remarks>
  31.                 ///
  32.                 private const string defaultPortalOrSite = "S";
  33.                 private string portalOrSite = defaultPortalOrSite;
  34.                 [Bindable(true),
  35.                 Category("Assembly Class Settings"),
  36.                 DefaultValue(defaultPortalOrSite),
  37.                 Description("Portal or Site Setting (P or S)")]
  38.                 public string PortalOrSite
  39.                 {
  40.                         get
  41.                         {
  42.                                 return portalOrSite;
  43.                         }
  44.  
  45.                         set
  46.                         {
  47.                                 portalOrSite = value;
  48.                         }
  49.                 }
  50.                 //
  51.                 //------------------------------------------------------------------------------------------
  52.                 /// <remarks>DefaultRedirUrl Property: Default Redirect Url (default if no user information
  53.                 /// is available </remarks>
  54.                 ///
  55.                 private const string defaultDefaultRedirUrl = "";
  56.                 private string defaultRedirUrl = defaultDefaultRedirUrl;
  57.                 [Bindable(true),
  58.                 Category("Assembly Class Settings"),
  59.                 DefaultValue(defaultDefaultRedirUrl),
  60.                 Description("Default Url to redirect to if no user info available")]
  61.                 public string DefaultRedirUrl
  62.                 {
  63.                         get
  64.                         {
  65.                                 return defaultRedirUrl;
  66.                         }
  67.  
  68.                         set
  69.                         {
  70.                                 defaultRedirUrl = value;
  71.                         }
  72.                 }
  73.  
  74.                 //
  75.                 //==========================================================================================
  76.                 /// <summary>
  77.                 /// CreateChildControls - Create the Child controls for this Control:
  78.                 /// </summary>
  79.                 //------------------------------------------------------------------------------------------
  80.                 protected override void CreateChildControls()
  81.                 {
  82.  
  83.                 }
  84.                 //
  85.                 //==========================================================================================
  86.                 /// <summary>
  87.                 /// Render - Render the HTML/controls:
  88.                 /// </summary>
  89.                 //------------------------------------------------------------------------------------------
  90.                 protected override void Render(HtmlTextWriter output)
  91.                 {
  92.                         // Make sure the control is ready:
  93.                         //
  94.                         EnsureChildControls();
  95.                         //
  96.                         SPSite TheCurSite = SPControl.GetContextSite(Context);
  97.                         //SPWeb BaseWeb = TheCurSite.AllWebs[0];
  98.                         SPWeb BaseWeb = TheCurSite.RootWeb;
  99.                         string Department = "";
  100.                         // Get the current web:
  101.                         SPWeb TheCurWeb = SPControl.GetContextWeb(Context);
  102.                         // Get the current (Active) user:
  103.                         bool FoundUser = false;
  104.  
  105.                         SPUser TheCurUserRef = TheCurWeb.CurrentUser;
  106.                         SPUser TheCurUser = null;
  107.                         try
  108.                         {
  109.                                 TheCurUser = BaseWeb.Users[TheCurUserRef.LoginName];
  110.                                 FoundUser = true;
  111.                         }
  112.                         catch (Exception ErrHere)
  113.                         {
  114.                                 TheCurUser = TheCurUserRef;
  115.                                 string SomeInfo = ErrHere.Message.ToString();
  116.                                 FoundUser = false;
  117.                         }
  118.                         //
  119.                         if (FoundUser || !FoundUser)
  120.                         {
  121.                                 // Get the Portal Context:
  122.                                 //
  123.                                 bool KeepGettingUPM = false;
  124.                                 SPSite PortSite = null;
  125.                                 PortalContext pc = null;
  126.  
  127.                                 try
  128.                                 {
  129.                                         PortSite = new SPSite(TheCurSite.MakeFullUrl(TheCurSite.PortalUrl));
  130.                                         pc = PortalApplication.GetContext(PortSite.ID);
  131.                                         KeepGettingUPM = true;
  132.                                 }
  133.                                 catch (Exception Msg1)
  134.                                 {
  135.                                         string Msg1text = Msg1.Message.ToString();
  136.                                         KeepGettingUPM = false;
  137.                                         this.Page.Response.Write("Portal Site or context error: " + Msg1text);
  138.                                 }
  139.                                 //
  140.                                 // Get the User Profile Manager:
  141.                                 //
  142.                                 UserProfileManager upm = null;
  143.                                 if (KeepGettingUPM)
  144.                                 {
  145.                                         try
  146.                                         {
  147.                                                 upm = new UserProfileManager(pc);
  148.                                         }
  149.                                         catch          { KeepGettingUPM = false; }
  150.                                 }
  151.                                 //
  152.                                 // Get the User's Profile:
  153.                                 //
  154.                                 try
  155.                                 {
  156.                                         UserProfile CurUserProfile = upm.GetUserProfile(TheCurUser.LoginName);
  157.                                         Department = (string)CurUserProfile["Department"];
  158.                                 }
  159.                                 catch (Exception UPGetErr)
  160.                                 {
  161.                                         //
  162.                                         // No user profile - this is a problem for normal users so we will redirect:
  163.                                         //
  164.                                         string TheMsg = UPGetErr.Message.ToString();
  165.                                         Department = "UNKNOWN";
  166.                                         this.Page.Response.Write("USER PROFILE INFORMATION WAS NOT FOUND = " + TheMsg);
  167.                                 }
  168.                                 string UrlToAdd = "";
  169.                                 if (PortalOrSite == "P")
  170.                                 {
  171.                                         UrlToAdd = "/sites/";
  172.                                 }
  173.                                 else
  174.                                 {
  175.                                         UrlToAdd = "/sites/";
  176.                                         //                         UrlToAdd = "/";
  177.                                 }
  178.  
  179.                                 if (Department == "blahdept1")
  180.                                 {
  181.                                         this.Page.Response.Redirect(UrlToAdd + "blahdept1/default.aspx");
  182.                                 }
  183.                                 if (Department == "blahdept2")
  184.                                 {
  185.                                         this.Page.Response.Redirect(UrlToAdd + "blahdept2/default.aspx");
  186.                                 }
  187.                                 if (Department == "blahdept3")
  188.                                 {
  189.                                         this.Page.Response.Redirect(UrlToAdd + "blahdept3/default.aspx");
  190.                                 }
  191.                                 if (Department == "blahdept4")
  192.                                 {
  193.                                         this.Page.Response.Redirect(UrlToAdd + "blahdept4/default.aspx");
  194.                                 }
  195.                                 if (Department == "blahdept5")
  196.                                 {
  197.                                         this.Page.Response.Redirect(UrlToAdd + "blahdept5/default.aspx");
  198.                                 }
  199.                         }
  200.                         //
  201.                         // Unknown Department or not one we are interested in - redirect to the default:
  202.                         //
  203.                         // If portal, do nothing...
  204.                         //
  205.                         if (PortalOrSite == "P")
  206.                         {
  207.                                 // Do nothing....
  208.                                 return;
  209.                         }
  210.                         //
  211.                         // Site - redirect to the default url:
  212.                         //
  213.                         if (PortalOrSite == "S")
  214.                         {
  215.                                 // Do nothing....
  216.                                 this.Page.Response.Redirect(DefaultRedirUrl);
  217.                                 return;
  218.                         }
  219.                         //
  220.                         // End of Render
  221.                 }
  222.                 ///===========================================================================================
  223.                 /// <summary>
  224.                 /// CreateIdentity
  225.                 /// </summary>
  226.                 /// <param name="User"></param>
  227.                 /// <param name="Domain"></param>
  228.                 /// <param name="Password"></param>
  229.                 /// <returns></returns>
  230.                 protected static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
  231.                 {
  232.                         // The Windows NT user token.
  233.                         IntPtr tokenHandle = new IntPtr(0);
  234.  
  235.                         const int LOGON32_PROVIDER_DEFAULT = 0;
  236.                         const int LOGON32_LOGON_NETWORK = 3;
  237.  
  238.                         tokenHandle = IntPtr.Zero;
  239.  
  240.                         // Call LogonUser to obtain a handle to an access token.
  241.                         bool returnValue = LogonUser(User, Domain, Password,
  242.                         LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
  243.                         ref tokenHandle);
  244.  
  245.                         if (false == returnValue)
  246.                         {
  247.                                 int ret = Marshal.GetLastWin32Error();
  248.                                 throw new Exception("LogonUser failed with error code: " +  ret);
  249.                         }
  250.  
  251.                         System.Diagnostics.Debug.WriteLine("Created user token: " + tokenHandle);
  252.  
  253.                         //The WindowsIdentity class makes a new copy of the token.
  254.                         //It also handles calling CloseHandle for the copy.
  255.                         WindowsIdentity id = new WindowsIdentity(tokenHandle);
  256.                         CloseHandle(tokenHandle);
  257.                         return id;
  258.                 }
  259.  
  260.                 [DllImport("advapi32.dll", SetLastError=true)]
  261.                 private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
  262.                 int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
  263.  
  264.                 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
  265.                 private extern static bool CloseHandle(IntPtr handle);
  266.         }
  267. }
Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>