protected void Page_Load(object sender, EventArgs e)
        {
            WindowsUser = Request.LogonUserIdentity.Name;
            string queryString = string.Format("it.Type='AuthExt' AND it.Key='{0}' AND it.Value='{1}'", AuthExtName, WindowsUser);
            CriticalResults.CriticalResultsEntityManager manager = new CriticalResultsEntityManager();
            CriticalResults.UserEntryEntity [] entries = manager.QueryUserEntryEntities(queryString, null, null);

            string userHostAddress = Utilities.GetIP4Address();

            if (entries.Count() == 1)
            {
                if (entries.First().User.Enabled == true)
                {
                    UserName = entries.First().User.UserName;
                    Session["UserName"] = UserName;
                    CriticalResults.TokenEntity[] currentTokens = manager.GetTokensForUser(UserName);
                    foreach (CriticalResults.TokenEntity t in currentTokens)
                    {
                        if (t.Ipv4 == userHostAddress)
                        {
                            TokenGuid = t.Token.ToString();
                            Session["Token"] = TokenGuid.ToString();
                        }
                    }
                    if (TokenGuid == "")
                    {

                        CriticalResults.TokenEntity token = manager.CreateToken(entries.First().User, userHostAddress);
                        TokenGuid = token.Token.ToString();
                        Session["Token"] = TokenGuid.ToString();
                    }
                    Response.AddHeader("REFRESH", "5;URL=ResultList.aspx");

                }
                else
                {
                    PageMessage = "The ANCR account associated with this Windows Login " + WindowsUser + " is disabled.  Please contact your System Administrator.";
                    message.InnerText = PageMessage;
                }

            }
            else if(entries.Count() > 1)
            {
                PageMessage = "Multiple ANCR accounts resolved to this Windows User.  Until this is resolved you may not login with your Windows User Account.  Please contact your System Administrator.";
                message.InnerText = PageMessage;
            }
            else
            {
                PageMessage = "No ANCR account can be found for " + WindowsUser + ".  Please contact your System Administrator.";
                message.InnerText = PageMessage;
            }
        }
        public static bool Authenticate(string authKey, string authValue, string userName, string userIP, TraceSource _Trace, out string outUserName, out string tokenString, out string message)
        {
            outUserName="";
            tokenString = "";
            CriticalResults.CriticalResultsEntityManager manager = new CriticalResultsEntityManager();
            bool hasPassword = false;
            string queryString = string.Format("it.Type='AuthExt' AND it.Key='{0}' AND it.Value='{1}'", authKey, authValue);
            // if userName is specified then we go from ANCR and authValue is password, overwise authValue is username of already authenticated user
            if (userName != null)
            {
                hasPassword = true;
                UserEntity user = manager.GetUser(userName);
                if (user == null)
                {
                    message = "Invalid username or password.";
                    return false;
                }
                queryString = string.Format("it.Type='AuthExt' AND it.Key='{0}' AND it.Value='{1}' AND it.User.Id={2}", authKey, authValue, user.Id);
            }

            CriticalResults.UserEntryEntity[] entries = manager.QueryUserEntryEntities(queryString, null, null);
            if (entries.Count() == 1)
            {
                UserEntity user=entries.First().User;
                if (!hasPassword)
                {
                    _Trace.TraceEvent(TraceEventType.Information, 0, "Lookup for username \"{0}\" mapped to \"{1}\".", authValue, user.UserName);
                }
                if (user.Enabled == true)
                {
                    outUserName = user.UserName;
                    CriticalResults.TokenEntity[] currentTokens = manager.GetTokensForUser(user.UserName);
                    foreach (CriticalResults.TokenEntity t in currentTokens)
                    {
                        if (t.Ipv4 == userIP)
                        {
                            t.UpdatedTime = DateTime.Now;
                            manager.SaveChanges();
                            tokenString = t.Token.ToString();
                            message = "";
                            return true;
                        }
                    }
                    CriticalResults.TokenEntity token = manager.CreateToken(user, userIP);
                    tokenString = token.Token.ToString();
                    message = "";
                    return true;
                }
                else
                {
                    _Trace.TraceEvent(TraceEventType.Warning, 0, "Found user \"{0}\" mapped to \"{1}\".  ANCR Account disabled.", authValue, user.UserName);
                    message = "Your account is disabled. Please contact your System Administrator.";
                    return false;
                }
            }
            else
            {
                if (entries.Count() > 1)
                {
                    _Trace.TraceEvent(TraceEventType.Warning, 0, "Found multiple users \"{0}\" (Count: {1})", authValue, entries.Count());
                    message = "Multiple ANCR users found for your username, until this is resolved you may not login with your credentials.  Please contact your System Administrator.";
                }
                else
                {
                    if (hasPassword)
                    {
                        _Trace.TraceEvent(TraceEventType.Warning, 0, "Invalid username or password \"{0}\"", userName);
                        message = "Invalid username or password.";
                    }
                    else
                    {
                        _Trace.TraceEvent(TraceEventType.Warning, 0, "No ANCR account found for  \"{0}\"", authValue);
                        message = "No ANCR user is found for your credentials. Please contact your System Administrator.";
                    }
                }
                return false;
            }
        }