Ejemplo n.º 1
0
        public BooleanResult AuthenticatedUserGateway(SessionProperties properties)
        {
            // this method shall perform some other tasks ...

            UserInformation userInfo = properties.GetTrackedSingle <UserInformation>();

            UInfo uinfo = CognitoAccessor.getUserInfo(userInfo.Username);

            if (uinfo != null)
            {
                m_logger.DebugFormat("AuthenticatedUserGateway: Uinfo: {0}", uinfo.ToString());
                foreach (string group in uinfo.groups)
                {
                    userInfo.AddGroup(new GroupInformation()
                    {
                        Name = group
                    });
                }
                properties.AddTrackedSingle <UserInformation>(userInfo);

                // and what else ??? :)
            }

            return(new BooleanResult()
            {
                Success = true
            });
        }
Ejemplo n.º 2
0
        public static UInfo parseResponse(string res)
        {
            UInfo u = new UInfo();

            using (StringReader strReader = new StringReader(res))
            {
                // reason why could not login (empty = can login)
                u.whyCannotLogin = strReader.ReadLine();
                u.uname          = strReader.ReadLine();
                if (u.uname == null)
                {
                    throw new Exception("Bad response arrived: " + res);
                }
                u.fullName = strReader.ReadLine();
                u.email    = strReader.ReadLine();
                u.groups   = strReader.ReadLine().Split(';');
                if (u.groups.Length == 1 && u.groups[0].Contains(";"))
                {
                    throw new Exception("Bad response arrived (groups wrong): " + res);
                }
            }

            return(u);
        }
Ejemplo n.º 3
0
        public static BooleanResult getResponse(String uname, String pwd)
        {
            try
            {
                // Create a request using a URL that can receive a post.
                WebRequest request = WebRequest.Create(Settings.resolveSettings());
                // Set the Method property of the request to POST.
                request.Method  = "POST";
                request.Timeout = 2000;
                // Create POST data and convert it to a byte array.
                string postData  = "{\"username\":\"" + uname + "\",\"password\":\"" + pwd + "\"}";
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/json";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;

                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                }

                // Get the response.
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        // Open the stream using a StreamReader for easy access.
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            // Read the content.
                            string responseFromServer = reader.ReadToEnd();
                            // Display the content.
                            m_logger.InfoFormat("Response: {0}", responseFromServer);
                            // save it for later use
                            if (resps.ContainsKey(uname))
                            {
                                resps.Remove(uname);
                            }
                            resps.Add(uname, UInfo.parseResponse(responseFromServer));
                        }
                    }
                }
                return(new BooleanResult()
                {
                    Success = true
                });
            }
            catch (WebException webx)
            {
                m_logger.ErrorFormat("Accessor.WebException: {0}", webx.Message);

                using (HttpWebResponse res = (HttpWebResponse)webx.Response)
                {
                    if (res != null)
                    {
                        using (StreamReader resReader = new StreamReader(res.GetResponseStream()))
                        {
                            string responseBody = resReader.ReadLine();
                            if (responseBody.Length > 0)
                            {
                                return(new BooleanResult()
                                {
                                    Success = false, Message = responseBody
                                });
                            }
                        }
                    }
                }

                return(new BooleanResult()
                {
                    Success = false, Message = webx.Message
                });
            }
            catch (Exception e)
            {
                // very bad scenario
                m_logger.ErrorFormat("Accessor.Exception: {0}", e.StackTrace);
                return(new BooleanResult()
                {
                    Success = false, Message = e.Message
                });
            }
        }