Skip to content

Instantly share code, notes, and snippets.

@milannankov
Last active September 19, 2016 16:18
Show Gist options
  • Select an option

  • Save milannankov/97fa0bd0c43d30911f4e7ebd50d2f626 to your computer and use it in GitHub Desktop.

Select an option

Save milannankov/97fa0bd0c43d30911f4e7ebd50d2f626 to your computer and use it in GitHub Desktop.
Debug Azure App Service Authentication Locally
private JwtSecurityToken GetAuthenticationTokenForUser(string username)
{
var claims = new Claim[]
{
new Claim(JwtRegisteredClaimNames.Sub, username)
};
var signingKey = this.GetSigningKey();
var audience = this.GetSiteUrl(); // audience must match the url of the site
var issuer = this.GetSiteUrl(); // audience must match the url of the site
JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
claims,
signingKey,
audience,
issuer,
TimeSpan.FromHours(24)
);
return token;
}
private bool IsPasswordValid(string username, string password)
{
// this is where we would do checks agains a database
return true;
}
private string GetSiteUrl()
{
var settings = this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
if (string.IsNullOrEmpty(settings.HostName))
{
return "http://localhost";
}
else
{
return "https://" + settings.HostName + "/";
}
}
private string GetSigningKey()
{
var settings = this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
if (string.IsNullOrEmpty(settings.HostName))
{
// this key is for debuggint and testing purposes only
// this key should match the one supplied in Startup.MobileApp.cs
return "GfYVqdtZUJQfghRiaonAeRQRDjytRi47";
}
else
{
return Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY");
}
}
MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();
if (string.IsNullOrEmpty(settings.HostName))
{
// This middleware is intended to be used locally for debugging. By default, HostName will
// only have a value when running in an App Service application.
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
{
SigningKey = "GfYVqdtZUJQfghRiaonAeRQRDjytRi47",
ValidAudiences = new[] { "http://localhost" },
ValidIssuers = new[] { "http://localhost" },
TokenHandler = config.GetAppServiceTokenHandler()
});
}
app.UseWebApi(config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment