예제 #1
0
        public override bool ValidateUser(string username, string password)
        {
            bool   isValid    = false;
            string dbpassword = "";

            try
            {
                GetAttributesRequest  request  = new GetAttributesRequest().WithDomainName(domain).WithItemName(username).WithAttributeName(new string[] { "Password", "PasswordAnswer" });
                GetAttributesResponse response = client.GetAttributes(request);
                if (response.IsSetGetAttributesResult())
                {
                    GetAttributesResult result = response.GetAttributesResult;
                    foreach (Attribute att in result.Attribute)
                    {
                        switch (att.Name)
                        {
                        case "Password": dbpassword = att.Value; break;

                        default: break;
                        }
                    }
                }
                else
                {
                    throw new MembershipPasswordException("User not found");
                }
                if (dbpassword == password)
                {
                    return(true);
                }
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "ValidateUser");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw e;
                }
            }

            return(isValid);
        }
예제 #2
0
        /// <summary>
        /// Get a single attribute back from the item.
        /// </summary>
        /// <param name="domainName"></param>
        /// <param name="itemName"></param>
        /// <param name="name"></param>
        /// <returns>Returns the value of the attribute if it exists, otherwise an empty string.</returns>
        /// <remarks>Can't do multiple as no guarantee as to order.</remarks>
        public string GetAttribute(string domainName, string itemName, string name)
        {
            var request = new GetAttributesRequest
            {
                DomainName    = domainName,
                ItemName      = itemName,
                AttributeName = new List <string> {
                    name
                }
            };

            GetAttributesResponse response = Client.GetAttributes(request);

            if (response.IsSetGetAttributesResult())
            {
                if (response.GetAttributesResult.Attribute.Count > 0)
                {
                    return(response.GetAttributesResult.Attribute[0].Value);
                }
            }

            return(string.Empty);
        }
예제 #3
0
        public override string ResetPassword(string username, string answer)
        {
            if (!EnablePasswordReset)
            {
                throw new NotSupportedException("Password reset is not enabled.");
            }

            if (answer == null && RequiresQuestionAndAnswer)
            {
                throw new ProviderException("Password answer required for password reset.");
            }

            string newPassword =
                System.Web.Security.Membership.GeneratePassword(6, 0);

            string passwordAnswer = "";

            try
            {
                GetAttributesRequest  request  = new GetAttributesRequest().WithDomainName(domain).WithItemName(username).WithAttributeName(new string[] { "Password", "PasswordAnswer" });
                GetAttributesResponse response = client.GetAttributes(request);
                if (response.IsSetGetAttributesResult())
                {
                    GetAttributesResult result = response.GetAttributesResult;
                    foreach (Attribute att in result.Attribute)
                    {
                        switch (att.Name)
                        {
                        case "PasswordAnswer": passwordAnswer = att.Value; break;

                        default: break;
                        }
                    }
                }
                else
                {
                    throw new MembershipPasswordException("User not found");
                }

                if (RequiresQuestionAndAnswer && !CheckPassword(answer, passwordAnswer))
                {
                    throw new MembershipPasswordException("Incorrect password answer.");
                }

                // Update the new password here
                ReplaceableAttribute replace  = new ReplaceableAttribute().WithName("Password").WithValue(newPassword).WithReplace(true);
                PutAttributesRequest prequest = new PutAttributesRequest().WithDomainName(domain).WithItemName(username).WithAttribute(replace);
                client.PutAttributes(prequest);
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "ResetPassword");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw e;
                }
            }

            return(newPassword);
        }
예제 #4
0
        //
        // MembershipProvider.GetPassword
        //

        public override string GetPassword(string username, string answer)
        {
            if (!EnablePasswordRetrieval)
            {
                throw new ProviderException("Password Retrieval Not Enabled.");
            }

            if (PasswordFormat == MembershipPasswordFormat.Hashed)
            {
                throw new ProviderException("Cannot retrieve Hashed passwords.");
            }

            string password       = "";
            string passwordAnswer = "";

            try
            {
                GetAttributesRequest  request  = new GetAttributesRequest().WithDomainName(domain).WithItemName(username).WithAttributeName(new string[] { "Password", "PasswordAnswer" });
                GetAttributesResponse response = client.GetAttributes(request);
                if (response.IsSetGetAttributesResult())
                {
                    GetAttributesResult result = response.GetAttributesResult;
                    foreach (Attribute att in result.Attribute)
                    {
                        switch (att.Name)
                        {
                        case "Password": password = att.Value; break;

                        case "PasswordAnswer": passwordAnswer = att.Value; break;

                        default: break;
                        }
                    }
                }
                else
                {
                    throw new MembershipPasswordException("User not found");
                }
            }
            catch (Exception e)
            {
                if (WriteExceptionsToEventLog)
                {
                    WriteToEventLog(e, "GetPassword");

                    throw new ProviderException(exceptionMessage);
                }
                else
                {
                    throw e;
                }
            }

            if (RequiresQuestionAndAnswer && !CheckPassword(answer, passwordAnswer))
            {
                throw new MembershipPasswordException("Incorrect password answer.");
            }

            if (PasswordFormat == MembershipPasswordFormat.Encrypted)
            {
                password = UnEncodePassword(password);
            }

            return(password);
        }