Friday, March 16, 2012

How to solve access denied problem on IIS 7?

Two ways:
#1 Manual
In file explorer, right click, goto security tab, Add IIS APPPOOL\DefaultAppPool into your folder


#2 Command line:
icacls c:\inetpub\wwwroot\YourPath /grant "IIS APPPOOL\DefaultAppPool":(OI)(CI)(RX)



 

How to set new password without old one in ASP.NET Membership?

Reset password, then use reseted password as old password to set new one.

                 MembershipUser user = Membership.GetUser(username);
                string oldpassword = user.ResetPassword();
                user.ChangePassword(oldpassword, "newpassword");


Credit Card validation for ASP.NET (Web Forms and MVC)

http://www.superstarcoders.com/blogs/posts/luhn-validation-for-asp-net-web-forms-and-mvc.aspx

LUHN Algorithm

The LUHN algorithm is a popular way to validate credit card numbers. I’ve used it many times while developing e-commerce applications to check that a user has entered their credit card number correctly. By using the LUHN algorithm to verify a card number, you can let a customer know their card number is invalid before taking payment through a gateway. After all, it’s a better user experience if they don’t have to wait for the server to try and authorize their card through a payment gateway with incorrect details that could have been detected using a simple LUHN check!
C#
public static class LuhnUtility
{
   public static bool IsCardNumberValid(string cardNumber, bool allowSpaces = false)
   {
      if (allowSpaces)
      {
         cardNumber = cardNumber.Replace(" ", "");
      }
      
      if (cardNumber.Any(c => !Char.IsDigit(c)))
      {
         return false;
      }

      int checksum = cardNumber
         .Select((c, i) => (c - '0') << ((cardNumber.Length - i - 1) & 1))
         .Sum(n => n > 9 ? n - 9 : n);

      return (checksum % 10) == 0 && checksum > 0;
   }
}
Javascript:
function isCardNumberValid(cardNumber, allowSpaces) {
   if (allowSpaces) {
      cardNumber = cardNumber.replace(/ /g, '');
   }

   if (!cardNumber.match(/^\d+$/)) {
      return false;
   }

   var checksum = 0;

   for (var i = 0; i < cardNumber.length; i++) {
      var n = (cardNumber.charAt(cardNumber.length - i - 1) - '0') << (i & 1);

      checksum += n > 9 ? n - 9 : n;
   }

   return (checksum % 10) == 0 && checksum > 0;
}