Example #1
0
        private async Task CreateOAuthTicket(OAuthCreatingTicketContext context)
        {
            var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

            var response = await context.Backchannel.SendAsync(request, context.HttpContext.RequestAborted);

            response.EnsureSuccessStatusCode();

            context.HttpContext.Response.Cookies.Append("token", context.AccessToken);

            var user = JObject.Parse(await response.Content.ReadAsStringAsync());

            context.RunClaimActions(user);

            LeagueContext leagueContext = context.HttpContext.RequestServices.GetService <LeagueContext>();
            int           id            = int.Parse((string)user["id"]);
            User          dbUser        = leagueContext.Users.Find(id);
            Role          role          = null;

            if (dbUser == null)
            {
                role   = leagueContext.Roles.First(r => r.Name == RoleConstants.User);
                dbUser = new User()
                {
                    Id        = id,
                    BattleTag = context.Identity.Name,
                    Role      = role
                };
                leagueContext.Users.Add(dbUser);
                leagueContext.SaveChanges();
            }
            if (dbUser.ProfileId == null)
            {
                Profile profile = await GetFullProfileAsync(dbUser, context, leagueContext);

                if (profile != null)
                {
                    leagueContext.Profiles.Add(profile);
                    dbUser.ProfileId = profile.Id;
                    leagueContext.Attach(dbUser);
                    leagueContext.Entry(dbUser).Property(u => u.ProfileId).IsModified = true;
                    leagueContext.SaveChanges();
                    SetHasProfileClaimAsTrue(context);
                }
            }
            else
            {
                SetHasProfileClaimAsTrue(context);
            }
            role = role ?? leagueContext.Roles.Find(dbUser.RoleId);
            context.Identity.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType, role.Name));
        }
        public virtual ActionResult Edit([FromForm] Match match)
        {
            if (!ModelState.IsValid)
            {
                match = LoadMatchWithPlayers(match.MatchId);
                return(View(match));
            }

            _leagueContext.Attach(match);
            _leagueContext.Entry(match).Property(m => m.FirstPlayerWins).IsModified  = true;
            _leagueContext.Entry(match).Property(m => m.SecondPlayerWins).IsModified = true;
            _leagueContext.SaveChanges();
            return(RedirectToAction(MVC.Season.Schedule()));
        }