예제 #1
0
        public static SecureString DecryptString(string encryptedData)
        {
            SecureString securedString   = new SecureString();
            string       encryptedString = Encrypting.EncryptString(securedString);

            try
            {
                byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
                    Convert.FromBase64String(encryptedData),
                    entropy,
                    System.Security.Cryptography.DataProtectionScope.LocalMachine);
                SecureString ss = ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
                Array.Clear(decryptedData, 0, decryptedData.Length);
                return(ss);
            }
            catch
            {
                return(new SecureString());
            }
        }
예제 #2
0
        public static string getAuthenticationID(string email, string psswd, string oflyAppID, string sharedSecret, string oldAuthID)
        {
            string oflyHashMeth  = "SHA1";
            string oflyTimestamp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz");

            string getAuthURL  = shutterflyHost + "/auth";
            string postAuthURL = shutterflyHost + "/user/" + email + "/auth?oflyAppId=" + oflyAppID;

            //Timestamp formatting
            int index = oflyTimestamp.LastIndexOf(':');

            oflyTimestamp = oflyTimestamp.Remove(index, 1).Insert(index, "");

            //Get API Signature
            string rawSignature = sharedSecret + "/user/" + email + "/auth?oflyAppId=" + oflyAppID + "&oflyHashMeth=" + oflyHashMeth + "&oflyTimestamp=" + oflyTimestamp;

            Byte[] encodedSignature = encoding.GetBytes(rawSignature);
            byte[] encryptedSignature;

            SHA1 sha = new SHA1CryptoServiceProvider();

            // This is one implementation of the abstract class SHA1.
            encryptedSignature = sha.ComputeHash(encodedSignature);

            string apiSignature = BitConverter.ToString(encryptedSignature);

            apiSignature = apiSignature.Replace("-", "");

            //Get existing authorization ID
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers.Add("oflyHashMeth", oflyHashMeth);
            headers.Add("oflyApiSig", apiSignature);
            headers.Add("Authorization", "SFLY user-auth=" + oldAuthID);
            headers.Add("oflyTimestamp", oflyTimestamp);

            string authenticationID = "";
            string result           = GetMethod(getAuthURL, headers);

            if (result != "")
            {
                XmlDocument atomXML = new XmlDocument();
                using (TextReader tr = new StringReader(result))
                {
                    atomXML.Load(tr);
                }
                if (atomXML.GetElementsByTagName("user:authToken").Count > 0)
                {
                    authenticationID = atomXML.DocumentElement["entry"].ChildNodes[9].InnerText;
                }
            }
            else
            {
                headers.Remove("Authorization");
                int counter = 0;
                do
                {
                    authenticationID = getNewAuthID(postAuthURL, psswd, headers);
                    counter++;
                }while (counter < 10 && authenticationID == "");
            }

            if (authenticationID != "")
            {
                //update to latest authentication ID
                SecureString newAuthIDSS = new SecureString();
                authenticationID.ToCharArray().ToList().ForEach(newAuthIDSS.AppendChar);
                string encryptedAuthID = Encrypting.EncryptString(newAuthIDSS);

                XmlDocument xml = new XmlDocument();
                xml.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                XmlNode appSettingsNode = xml.SelectSingleNode("configuration/appSettings");
                // Attempt to locate the requested setting.
                foreach (XmlNode childNode in appSettingsNode)
                {
                    if (childNode.Attributes["key"].Value == "shutterFlyAuthID")
                    {
                        childNode.Attributes["value"].Value = encryptedAuthID;
                    }
                }

                result = authenticationID;
            }
            else
            {
                result = "Failed: no authentication ID.";
            }

            return(result);
        }