Created
June 15, 2012 15:49
-
-
Save ericmann/2937180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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