Last active
September 28, 2017 15:38
-
-
Save kevinhillinger/b93a6375e535e528831f5991f47c0769 to your computer and use it in GitHub Desktop.
Lookup FQDN of Application Gateway for backend App Services Web App - asp.net core
This file contains 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
public class AzureSettings | |
{ | |
public IAzure Azure { get; set; } | |
} |
This file contains 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
public string Get(string id) | |
{ | |
var forwardedFor = HttpContext.Request.Headers["X-Forwarded-For"]; | |
var gatewayIp = forwardedFor == StringValues.Empty ? null | |
: forwardedFor.Last().Split(':').First(); | |
if (string.IsNullOrEmpty(gatewayIp)) //no gw | |
{ | |
return HttpContext.Request.Host.Value; | |
} | |
var fqdn = GetFqdn(gatewayIp); | |
//TODO: if needed, do a DNS lookup on the CNAME set to this FQDN or use a custom hashtable lookup | |
return fqdn; | |
} | |
// make sure you target the resource group of the app. Store in config settings | |
public string GetFqdn(string gatewayIp) | |
{ | |
var ips = azure.Networks.Manager.PublicIPAddresses.ListByResourceGroup("gw-testing"); | |
var publicIp = ips.First(ip => ip.IPAddress == gatewayIp); | |
return publicIp.Fqdn; | |
} |
This file contains 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
// Add to startup. azureauth.properties file in root | |
// see https://docs.microsoft.com/en-us/azure/virtual-machines/windows/csharp | |
// install nuget Microsoft.Azure.Management.Fluent | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
// Add framework services. | |
services.AddMvc(); | |
services.AddOptions(); | |
services.Configure<AzureSettings>(s => | |
{ | |
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Path.Combine(ContentRootPath, "azureauth.properties")); | |
var azure = Azure | |
.Configure() | |
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) | |
.Authenticate(credentials) | |
.WithDefaultSubscription(); | |
s.Azure = azure; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment