1

I've two get methods in my WebAPI, which are:

  1. CheckPassword(string Password, string regExp) //to validate password pattern, takes password and the regular expression.

  2. CheckEmailValid(string Email, string regExp) //to validate email pattern, takes email and its regular expression.

When I try to call a Get method in WebAPI by passing two strings(password, regExp) OR (email, regExp) using a console app, some of the character miss out from the string. But at the same time if I have the regular expression in my WebAPI and test, it works fine. So I miss few character somewhere between the application and the webAPI.

Original Regex string for Password

public const string passwordPattern = @"^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]{7,}$";

The string which I receive at WebAPI Method while debugging

{^(?=.*[0-9] .*)(?=.*[a-zA-Z] .*)[0-9a-zA-Z]{7,}$}

Here I'm missing out + symbol, thats why password validation fails all the time.

Original Regex string for Email

public const string emailPattern = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";

The string which I receive at WebAPI Method while debugging

\\A(?:[a-z0-9!

Any help will be greatly appreciated.

Jilu
  • 67
  • 1
  • 2
  • 12
  • 1
    for first `^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z]{7,}$` will suffice – rock321987 May 30 '16 at 06:13
  • Shud take atleast one Capital letter. but it accepts if there is no capital letter – Jilu May 30 '16 at 06:52
  • 1
    in that case use:- `^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[0-9a-zA-Z]{7,}$`..for at least one uppercase, one lowercase and one digit – rock321987 May 30 '16 at 06:54
  • 1
    While the regex really does not need any `+` quantifier, it is interesting what to do if the `+` quantifier *is* necessary. I think there is some Uri escaping in your code. Otherwise, it can be closed as a dupe of http://stackoverflow.com/questions/1559751/regex-to-make-sure-that-the-string-contains-at-least-one-lower-case-char-upper – Wiktor Stribiżew May 30 '16 at 06:56

1 Answers1

0

Performing URL Encoding actually solved my issue

While sending from APP to API, URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as a question mark (?), ampersand (&), slash mark (/), and spaces might be truncated or corrupted by some browsers. As a result, these characters must be encoded in tags or in query strings where the strings can be re-sent by a browser in a request string. This method is a convenient way to access the HttpUtility.UrlEncode method at run time from an ASP.NET application. Internally, this method uses HttpUtility.UrlEncode to encode strings. So, encode the regex string using HttpUtility.UrlEncode.

string urlEncodedPwdPattern = HttpUtility.UrlEncode(passwordPattern)

Pass the string to API.

HttpResponseMessage response = await client.GetAsync("api/Credential/CheckPassword?password=" + password + "&regExp=" + urlEncodedPwdPattern).ConfigureAwait(true);
Jilu
  • 67
  • 1
  • 2
  • 12