示例#1
0
        public void AthenticateUser()
        {
            string token = "";

            //try getting token from the Header
            IEnumerable <string> values = null;

            try{ values = HttpContext.Current.Request.Headers.GetValues(KeyWords.XAuthToken); }catch {}
            if (values != null)
            {
                try
                {
                    token = values.First();
                }
                catch (Exception e) { }
            }
            //try getting token from cookie in Header
            else
            {
                try
                {
                    IEnumerable <string> cookies = HttpContext.Current.Request.Headers.GetValues("Cookie");
                    string info = KeyWords.CookieToken + "=";
                    foreach (string cookie in cookies)
                    {
                        if (cookie.Contains(info))
                        {
                            string tok = cookie.Split(';').Where((i => i.Contains(info))).First().Trim();
                            token = tok.Remove(tok.IndexOf(info), info.Length);
                        }
                    }
                }
                catch { }
            }
            //try getting token from the URI:
            if (HttpContext.Current.Request.Url != null && String.IsNullOrEmpty(token))
            {
                try
                {
                    NameValueCollection col = HttpContext.Current.Request.Url.ParseQueryString();
                    token = col.Get("token");
                }
                catch { }
            }


            if (!String.IsNullOrEmpty(token))
            {
                try
                {
                    Token = token;
                    var userAccess = Keystone.Authenticate(token);
                    userid      = userAccess.User.Id;
                    user_name   = userAccess.User.Name;
                    IsValidUser = true;
                }
                catch { IsValidUser = false; }
            }
            HasAthenticatedWithActivityInfo = false;
        }
示例#2
0
        /// Upload table
        public HttpResponseMessage uploadTest(ApiController api, string queryType, string positionType, string casjobsMessage)
        {
            try
            {
                IEnumerable <string> values;
                if (api.ControllerContext.Request.Headers.TryGetValues(KeyWords.XAuthToken, out values))
                {
                    // Keystone authentication
                    string token      = values.First();
                    var    userAccess = Keystone.Authenticate(token);

                    Dictionary <String, String> dictionary = api.Request.GetQueryNameValuePairs().ToDictionary(kv => kv.Key, kv => kv.Value, StringComparer.OrdinalIgnoreCase);
                    String query = "";

                    string radiusDefault = "1";// in arcminutes
                    try { radiusDefault = float.Parse(dictionary["radiusDefault"]).ToString(); }
                    catch { }

                    if (dictionary["radecTextarea"] != null)
                    {
                        UploadDataReader up = new UploadDataReader(radiusDefault);
                        query += up.UploadTo(dictionary["radecTextarea"], queryType, dictionary["nearBy"]);
                    }
                    else
                    {
                        var task = api.Request.Content.ReadAsStreamAsync();
                        task.Wait();
                        Stream stream = task.Result;

                        using (UploadDataReader up = new UploadDataReader(new StreamReader(stream), radiusDefault))
                        {
                            query += up.UploadTo(queryType, dictionary["nearBy"]);
                        }
                    }

                    HttpResponseMessage resp = new HttpResponseMessage();
                    query += QueryTools.BuildQuery.buildQuery(queryType, dictionary, positionType);
                    //RunCasjobs run = new RunCasjobs();
                    //resp.Content = new StringContent(run.postCasjobs(query, token, casjobsMessage).Content.ReadAsStringAsync().Result);
                    return(resp);
                }
                else
                {
                    // No authentication (anonymous) // Logg
                    throw new UnauthorizedAccessException("Check the token you are using.");
                }
            }
            catch (Exception exp) {
                throw new Exception("Exception while uploading data to create temp table." + exp.Message);
            }
        }
        // GET api/keystoneauth
        public HttpResponseMessage Get()
        {
            IEnumerable <string> values;

            if (ControllerContext.Request.Headers.TryGetValues("X-Auth-Token", out values))
            {
                // Keystone authentication
                string token      = values.First();
                var    userAccess = Keystone.Authenticate(token);
                return(ControllerContext.Request.CreateResponse(HttpStatusCode.OK, "Keystone authentication"));
            }
            else
            {
                // No authentication (anonymous)
                return(ControllerContext.Request.CreateResponse(HttpStatusCode.OK, "Anonymous"));
            }
        }
示例#4
0
        public UserHistory(Dictionary <string, string> requestDir, Dictionary <string, string> ExtraInfo, HttpRequest Request)
        {
            if (requestDir.Keys.Count == 0)
            {
                throw new ArgumentException("Request has no input parameters.");
            }

            foreach (string key in requestDir.Keys)
            {
                string keyL = key.ToLower();

                if (keyL == "limit")
                {
                    if (!String.IsNullOrEmpty(requestDir[key]))
                    {
                        try { limit = Convert.ToInt64(requestDir[key]); }
                        catch { throw new Exception("The row limit parameter does not have a valid numerical value."); }
                        if (limit <= 0 || limit > Int64.Parse(KeyWords.MaxRows))
                        {
                            throw new Exception("The row limit parameter has to be an integer greater than 0 and smaller than " + Int64.Parse(KeyWords.MaxRows));
                        }
                    }
                }
                if (keyL == "date_low")
                {
                    if (!String.IsNullOrEmpty(requestDir[key]))
                    {
                        try { Time1 = DateTime.Parse(requestDir[key]); }
                        catch { throw new Exception("Lower date limit does not have a valid format"); }
                    }
                }
                if (keyL == "date_high")
                {
                    if (!String.IsNullOrEmpty(requestDir[key]))
                    {
                        try { Time2 = DateTime.Parse(requestDir[key]); }
                        catch { throw new Exception("Upper date limit does not have a valid format"); }
                    }
                }
                if (keyL == "token")
                {
                    try { UriToken = requestDir[key]; }
                    catch {}
                }
                if (keyL == "application")
                {
                    try { Application = requestDir[key]; }
                    catch {}
                }
                if (keyL == "format")
                {
                    try { format = requestDir[key]; }
                    catch {}
                }
                if (keyL == "doshowallhistory")
                {
                    try { DoShowAllHistory = requestDir[key] == "true" ? true : false; }
                    catch { }
                }
                if (keyL == "contentsearchtext")
                {
                    try { ContentSearchText = requestDir[key]; }
                    catch { }
                }
            }

            if (Request.Headers.AllKeys.Contains(KeyWords.XAuthToken))
            {
                HeaderToken = Request.Headers.Get(KeyWords.XAuthToken);
            }

            //Authenticating the Token. Token in header has priority over token in URI
            if (!String.IsNullOrEmpty(HeaderToken))
            {
                try
                {
                    var userAccess = Keystone.Authenticate(HeaderToken);
                    UserID = userAccess.User.Id;
                }
                catch { };
            }
            else if (!String.IsNullOrEmpty(UriToken))
            {
                try
                {
                    var userAccess = Keystone.Authenticate(UriToken);
                    UserID = userAccess.User.Id;
                }
                catch { };
            }
            else
            {
                throw new UnauthorizedAccessException("Unable to find user. Token was not provided.");
            }

            if (String.IsNullOrEmpty(UserID))
            {
                throw new UnauthorizedAccessException("Unable to find user. Token could not be authenticated. Try getting a new token.");
            }

            try
            {
                using (oConn = new SqlConnection(KeyWords.SciserverLogDBconnection))
                {
                    oConn.Open();
                    SqlCommand Cmd = BuildCommand(oConn);
                    query = Cmd.CommandText;
                    Cmd.CommandTimeout = Int32.Parse(KeyWords.DatabaseSearchTimeout);
                    var Adapter = new SqlDataAdapter(Cmd);
                    Adapter.Fill(ResultDataSet);
                    oConn.Close();
                }
            }
            catch { throw; }
            PrepareResultSet(ref ResultDataSet, requestDir);
        }