public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Entering GetXboxLiveAuth"); string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var client = new HttpClient(); var xboxAuth = new XboxLiveAuthorizer(client); try { var info = await xboxAuth.GetAuthInfoAsync(requestBody); var infoJson = JsonSerializer.Serialize(info); var result = new AuthResultViewModel { Token = Crypto.Encrypt(infoJson), Gamertag = info.Gamertag, Expiration = info.Expiration }; return(new OkObjectResult(result)); } catch (Exception e) { log.LogError(e, "Exception during token retrieval"); return(new UnauthorizedResult()); } }
public static AuthResultViewModel BuildToken(string email, string secretKey, long expireMinutes, string issuer) { var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512); DateTime expiry = DateTime.UtcNow.AddMinutes(expireMinutes); var token = new JwtSecurityToken( issuer, issuer, GetTokenClaims(email), expires: expiry, signingCredentials: creds ); var result = new AuthResultViewModel() { Authenticated = true, Created = DateTime.Now, ExpiresIn = expireMinutes * 60, AccesToken = new JwtSecurityTokenHandler().WriteToken(token) }; return(result); }