public virtual async Task <XasTokenResponse> GetXTokenAsync(string sandbox, bool forceRefresh) { if (this.AuthContext == null) { throw new InvalidOperationException("User Info is not found."); } XasTokenResponse xToken = null; if (!forceRefresh) { // return cached token if we have one and didn't expire string cacheKey = AuthTokenCache.GetCacheKey(this.AuthContext.UserName, this.AuthContext.AccountSource, this.AuthContext.Tenant, string.Empty, sandbox); this.XTokenCache.Value.TryGetCachedToken(cacheKey, out xToken); } if (xToken == null) { var msaToken = await this.AuthContext.AcquireTokenSilentAsync(); var xstsToken = await this.FetchXstsToken(msaToken, sandbox); xToken = xstsToken; } return(xToken); }
public async Task <TestAccount> SignInTestAccountAsync(string sandbox) { if (this.AuthContext == null) { throw new InvalidOperationException("User Info is not found."); } if (this.AuthContext.AccountSource == DevAccountSource.XboxDeveloperPortal) { throw new InvalidOperationException("XDP logins are deprecated, please use a Partner Center or Test account"); } if (this.AuthContext.AccountSource == DevAccountSource.WindowsDevCenter) { throw new InvalidOperationException("To log in a Partner Center account, call the SignInAsync method"); } string msaToken = await this.AuthContext.AcquireTokenAsync(); XasTokenResponse token = await this.FetchXstsToken(msaToken, sandbox); var account = new TestAccount(token); return(account); }
public async Task <DevAccount> SignInAsync(string tenant) { if (this.AuthContext == null) { throw new InvalidOperationException("User Info is not found."); } if (this.AuthContext.AccountSource == DevAccountSource.XboxDeveloperPortal) { throw new InvalidOperationException("XDP logins are deprecated, please use a Partner Center or Test account"); } if (this.AuthContext.AccountSource == DevAccountSource.TestAccount) { throw new InvalidOperationException("To log in a test account, call the SignInTestAccountAsync method"); } string aadToken = await this.AuthContext.AcquireTokenAsync(); XasTokenResponse token = await this.FetchXdtsToken(aadToken, string.Empty, null); var account = new DevAccount(token, this.AuthContext.AccountSource, tenant); return(account); }
public void UpdateToken(string key, XasTokenResponse token) { lock (TokenLock) { string cacheFilePath = Path.Combine(ClientSettings.Singleton.CacheFolder, this.cacheFile); this.CachedTokens[key] = token; File.WriteAllText(cacheFilePath, JsonConvert.SerializeObject(this.CachedTokens)); } }
internal TestAccount(XasTokenResponse xtoken) { object xui = null; xtoken.DisplayClaims?.TryGetValue("xui", out xui); if (xui == null) { return; } JArray array = xui as JArray; if (array == null) { return; } JObject list = array[0] as JObject; if (list == null) { return; } if (list.ContainsKey("agg")) { this.AgeGroup = list["agg"].ToString(); } if (list.ContainsKey("gtg")) { this.Gamertag = list["gtg"].ToString(); } if (list.ContainsKey("prv")) { this.PrivilegeString = list["prv"].ToString(); } if (list.ContainsKey("usr")) { this.RestrictedPrivilegeString = list["usr"].ToString(); } if (list.ContainsKey("xid")) { this.Xuid = list["xid"].ToString(); } if (list.ContainsKey("uhs")) { this.UserHash = list["uhs"].ToString(); } }
public bool TryGetCachedToken(string key, out XasTokenResponse token) { token = null; if (this.CachedTokens.TryGetValue(key, out XasTokenResponse cachedToken) && cachedToken != null && !string.IsNullOrEmpty(cachedToken.Token) && cachedToken.NotAfter >= DateTime.UtcNow) { Log.WriteLog($"Using token from cache for {key}."); token = cachedToken; return(true); } return(false); }
protected async Task <XasTokenResponse> FetchXstsToken(string msaToken, string sandbox) { // Get XASU token XasTokenResponse token = null; using (var tokenRequest = new XboxLiveHttpRequest()) { HttpResponseMessage response = (await tokenRequest.SendAsync(() => { var requestMsg = new HttpRequestMessage(HttpMethod.Post, ClientSettings.Singleton.XASUEndpoint); XasuTokenRequest xasuTokenRequest = new XasuTokenRequest(); xasuTokenRequest.Properties["SiteName"] = "user.auth.xboxlive.com"; xasuTokenRequest.Properties["RpsTicket"] = $"d={msaToken}"; var requestContent = JsonConvert.SerializeObject(xasuTokenRequest); requestMsg.Content = new StringContent(requestContent); requestMsg.Content.Headers.ContentType.MediaType = "application/json"; return(requestMsg); })).Response; // Get XASU token with MSA token response.EnsureSuccessStatusCode(); Log.WriteLog("Fetch XASU token succeeded."); token = await response.Content.DeserializeJsonAsync <XasTokenResponse>(); } // Get XSTS token using (var tokenRequest = new XboxLiveHttpRequest()) { HttpResponseMessage response = (await tokenRequest.SendAsync(() => { var requestMsg = new HttpRequestMessage(HttpMethod.Post, ClientSettings.Singleton.XSTSEndpoint); XstsTokenRequest xstsTokenRequest = new XstsTokenRequest(sandbox) { RelyingParty = "http://xboxlive.com" }; xstsTokenRequest.Properties["UserTokens"] = new[] { token.Token }; var requestContent = JsonConvert.SerializeObject(xstsTokenRequest); requestMsg.Content = new StringContent(requestContent); requestMsg.Content.Headers.ContentType.MediaType = "application/json"; return(requestMsg); })).Response; // Get XASU token with MSA token response.EnsureSuccessStatusCode(); Log.WriteLog("Fetch XSTS token succeeded."); token = await response.Content.DeserializeJsonAsync <XasTokenResponse>(); } string key = AuthTokenCache.GetCacheKey(this.AuthContext.UserName, this.AuthContext.AccountSource, this.AuthContext.Tenant, string.Empty, sandbox); this.XTokenCache.Value.UpdateToken(key, token); return(token); }
internal static string PrepareForAuthHeader(XasTokenResponse token) { TestAccount ta = new TestAccount(token); return($"XBL3.0 x={ta.UserHash};{token.Token}"); }