Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 2.49 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Global.asax - WindowsAuthentication_Authenticate - SessionState null and Multiple Calls
  2. <link href="@Url.Stylesheet("Common.css")" rel="stylesheet" type="text/css" />
  3. <script src="@Url.Script("Tools/extensions.js")" type="text/javascript"></script>
  4.        
  5. protected void WindowsAuthentication_Authenticate(object sender, WindowsAuthenticationEventArgs e)
  6.     {
  7.  
  8.         bool isUserFound = false;
  9.         bool isUserGone = false;
  10.         UtilityService utility = new UtilityService();
  11.  
  12.         string rawurl = HttpContext.Current.Request.RawUrl;
  13.  
  14.         if (e.Identity.IsAuthenticated)
  15.         {
  16.  
  17.             if (userPlaceHolder == null) //local property, work around for session problem
  18.             {
  19.                 UserInternal userInternal = utility.GetLoggedInUser(e.Identity.Name);
  20.                 if (userInternal != null)
  21.                 {
  22.                     userPlaceHolder = userInternal;
  23.                     isUserFound = true;
  24.                     //TODO: check for gone, you can use userInternal or userPlaceHolder
  25.                     isUserGone = false;
  26.                 }
  27.             }
  28.             else
  29.             {
  30.                 isUserFound = true;
  31.                 //TODO: check for gone, use userPlaceHolder
  32.                 isUserGone = false;
  33.             }
  34.         }
  35.  
  36.         //set the user
  37.         if (isUserFound && !isUserGone)
  38.         {
  39.             string role = String.IsNullOrWhiteSpace(userPlaceHolder.UserClass) ? String.Empty : userPlaceHolder.UserClass;
  40.             // Setting the current user and role in the Principal
  41.             e.User = new System.Security.Principal.GenericPrincipal(e.Identity, new string[] { role });
  42.             HttpContext.Current.User = e.User;
  43.         }
  44.  
  45.         //handle gone and user not found
  46.         if (!rawurl.LowerInvariantContains(ControllerNames.Message) &&
  47.             !rawurl.LowerInvariantContains(".css") &&
  48.             !rawurl.LowerInvariantContains(".js") &&
  49.             !rawurl.LowerInvariantContains(".jpg") &&
  50.             !rawurl.LowerInvariantContains(".gif") &&
  51.             !rawurl.LowerInvariantContains(".png"))
  52.         {
  53.             if (!isUserFound || isUserGone)
  54.             {
  55.                 string url = String.Empty;
  56.                 if (isUserGone)
  57.                     url = utility.GetSiteRestrictedLink(ProcessingMessagesEnum.UserLocked);
  58.                 else
  59.                     url = utility.GetSiteRestrictedLink(ProcessingMessagesEnum.UserNotAuthorizedToViewSite); ;
  60.                 HttpContext.Current.Response.Redirect(url);
  61.             }
  62.         }
  63.     }