Last active
September 19, 2016 16:18
-
-
Save milannankov/97fa0bd0c43d30911f4e7ebd50d2f626 to your computer and use it in GitHub Desktop.
Debug Azure App Service Authentication Locally
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
| 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"); | |
| } | |
| } |
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
| 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