Skip to content

Instantly share code, notes, and snippets.

@ericmann
Created June 15, 2012 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmann/2937180 to your computer and use it in GitHub Desktop.
Save ericmann/2937180 to your computer and use it in GitHub Desktop.
// How can I rewrite this to perform multiple checks in a row without repeating if (_allowed.IPs.Contains(userIPAddress)) { authentic = true; goto result; } every time?
public class Authentication {
private List<string> _allowedIPs;
public bool IsAuthentic(string userIPAddress) {
bool authentic = false;
if (_allowedIPs == null)
_allowedIPs = new List<string>();
if (_allowed.IPs.Contains(userIPAddress)) { authentic = true; goto result; }
string whitelist = ConfigurationManager.AppSettings["IPAddressWhiteList"];
List<string> allowedIPs = whitelist.Split(',').ToList<string>();
allowedIPs.ForEach(aip => { if (!_allowedIPs.Contains(aip)) _allowedIPs.Add(aip); });
// Now that the list is populated with hard-coded IP addresses, check again
if (_allowed.IPs.Contains(userIPAddress)) { authentic = true; goto result; }
if (RoleEnvironment.IsAvailable)
{
var serverIP = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpIn"].IPEndpoint.Address.ToString();
allowedIPs.Add(serverIP);
}
// If the request came from the same server address, return
if (_allowed.IPs.Contains(userIPAddress)) { authentic = true; goto result; }
if (!string.IsNullOrEmpty(Util.Azure.StagingIP))
{
allowedIPs.Add(Util.Azure.StagingIP);
}
// If the request came from the staging instance, return
if (_allowed.IPs.Contains(userIPAddress)) { authentic = true; goto result; }
if (!string.IsNullOrEmpty(Util.Azure.ProductionIP))
{
allowedIPs.Add(Util.Azure.ProductionIP);
}
// If the result came from the production instance, return
if (_allowed.IPs.Contains(userIPAddress)) { authentic = true; }
result:
return authentic;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment