예제 #1
0
        public string GetAccountCharacters()
        {
            try
            {
                Response.ContentType = "application/json";

                if (!User.Identity.IsAuthenticated)
                {
                    Response.StatusCode = 500;
                    return(JsonConvert.SerializeObject(new { ErrorMessage = "User is not authenticated" }));
                }

                string          token        = UserToken;
                BNetCharacter[] accountChars = HttpContext.Cache[token + ACCOUNT_CHAR_CACHE_KEY] as BNetCharacter[];
                if (accountChars == null)
                {
                    accountChars = BNetContext.GetAccountCharacters(UserRegion, token).ToArray();

                    if (accountChars == null)
                    {
                        Response.StatusCode = 404;
                        return(JsonConvert.SerializeObject(new { ErrorMessage = "Unable to retrieve account characters" }));
                    }

                    HttpContext.Cache.Add(
                        token + ACCOUNT_CHAR_CACHE_KEY,
                        accountChars,
                        null,
                        DateTime.Now.AddSeconds(Config.AccountCharacterCacheSeconds),
                        TimeSpan.Zero,
                        System.Web.Caching.CacheItemPriority.BelowNormal,
                        null);
                }

                return(JsonConvert.SerializeObject(new { ErrorMessage = "", Toons = accountChars.OrderByDescending(x => x.Level).ThenBy(x => x.Realm) }));
            }
            catch (Exception ex)
            {
                LogError(ex);
                Response.StatusCode = 500;
                return(JsonConvert.SerializeObject(new { ErrorMessage = "System error occured" }));
            }
        }
예제 #2
0
        private BNetCharacterDetails GetCharacterDetails(CharacterDefinition toon)
        {
            string toonKey = toon.Region + toon.Server + toon.Name + CHAR_DETAILS_CACHE_KEY;
            BNetCharacterDetails charDets = HttpContext.Cache[toonKey] as BNetCharacterDetails;

            if (charDets == null)
            {
                charDets = BNetContext.GetCharacterDetails(toon.Region, toon.Server, toon.Name);
                if (charDets != null)
                {
                    HttpContext.Cache.Add(
                        toonKey,
                        charDets,
                        null,
                        DateTime.Now.AddSeconds(Config.CharacterDetailsCacheSeconds),
                        TimeSpan.Zero,
                        System.Web.Caching.CacheItemPriority.BelowNormal,
                        null);
                }
            }
            return(charDets);
        }
예제 #3
0
 private void OnNodeClick(BNetContext<HeroSummary> d3Object)
 {
     guiBattleNetHostList.SelectedItem = hosts.FirstOrDefault(h => h.Url == d3Object.Host);
     guiBattleTag.Text = d3Object.BattleTag.ToString();
     D3ObjectLiveUrl.Text = D3Api.GetHeroUrlFromHeroId(d3Object.BattleTag, d3Object.Data.Id);
 }
예제 #4
0
 private void OnNodeClick(BNetContext<Career> d3Object)
 {
     guiBattleNetHostList.SelectedItem = hosts.FirstOrDefault(h => h.Url == d3Object.Host);
     guiBattleTag.Text = d3Object.BattleTag.ToString();
     D3ObjectLiveUrl.Text = D3Api.GetCareerUrl(d3Object.BattleTag);
 }
예제 #5
0
        public ActionResult Token(string state, string code)
        {
            OAuthTokenForm form = new OAuthTokenForm();

            if (string.IsNullOrWhiteSpace(state) || string.IsNullOrWhiteSpace(code) || Session[STATE_SESSION_KEY] == null || state != Session[STATE_SESSION_KEY] as string)
            {
                form.ErrorMessage = "WoW Loremaster was not granted access to your WoW profile.";
            }
            else
            {
                var region   = state.Split('_')[1];
                var postBody = string.Format("redirect_uri={0}&scope={1}&grant_type=authorization_code&code={2}",
                                             Config.OAuthRedirectUrl, Config.OAuthScope, code);
                var postBytes = System.Text.Encoding.ASCII.GetBytes(postBody);


                HttpWebRequest tokenRequest = (HttpWebRequest)WebRequest.Create(Config.OAuthBaseUrl(region) + "/oauth/token");
                tokenRequest.Headers.Add("Authorization", "Basic " + System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(Config.AuthApiKey + ":" + Config.AuthApiSecret)));
                tokenRequest.ContentType   = "application/x-www-form-urlencoded";
                tokenRequest.ContentLength = postBytes.Length;
                tokenRequest.Method        = "POST";
                using (Stream body = tokenRequest.GetRequestStream())
                {
                    body.Write(postBytes, 0, postBytes.Length);
                }

                var resp = "";

                using (HttpWebResponse tokenResponse = (HttpWebResponse)tokenRequest.GetResponse())
                    using (StreamReader reader = new StreamReader(tokenResponse.GetResponseStream()))
                    {
                        resp = reader.ReadToEnd();
                    }

                if (!string.IsNullOrWhiteSpace(resp))
                {
                    JObject respObject = JObject.Parse(resp);

                    var accessToken            = (string)respObject["access_token"];
                    var tokenExpirationSeconds = (int)respObject["expires_in"];
                    var scope = (string)respObject["scope"];

                    if (string.IsNullOrWhiteSpace(accessToken) || string.IsNullOrWhiteSpace(scope) || tokenExpirationSeconds < 0)
                    {
                        form.ErrorMessage = "Unable to retreive account authorization.";
                    }
                    else
                    {
                        if (scope != Config.OAuthScope)
                        {
                            form.ErrorMessage = "You must allow Loremaster Helper to access your wow profile to use this service.";
                        }
                        else
                        {
                            var bnetUser = BNetContext.GetUser(region, accessToken);
                            if (bnetUser == null)
                            {
                                form.ErrorMessage = "Unable to retreive account authorization.";
                            }
                            else
                            {
                                var identity = new ClaimsIdentity(
                                    new[]
                                {
                                    new Claim(ClaimTypes.Name, bnetUser.Id),
                                    new Claim(ClaimTypes.Locality, region),
                                    new Claim(ClaimTypes.SerialNumber, accessToken),
                                    new Claim(ClaimTypes.GivenName, bnetUser.BattleTag)
                                },
                                    DefaultAuthenticationTypes.ApplicationCookie);
                                AuthContext.SignIn(new AuthenticationProperties()
                                {
                                    IsPersistent = true,
                                    ExpiresUtc   = DateTime.SpecifyKind(DateTime.Now.AddSeconds(tokenExpirationSeconds), DateTimeKind.Utc)
                                }, identity);

                                return(RedirectToAction("Calculator", "Home", new { region = region }));
                            }
                        }
                    }
                }
                else
                {
                    form.ErrorMessage = "Unable to retreive account authorization.";
                }
            }
            return(View(form));
        }