Ejemplo n.º 1
0
        private async Task <AuthResponse> GetAuthResponseAsync(
            DateTime tokenExpiration,
            PlayFabResult playFabResult = null,
            bool newlyCreated           = false,
            string errorMessage         = null,
            ClaimsIdentity identity     = null,
            AuthenticationProperties authenticationProperties = null)
        {
            var localTokenExpiration = TimeZoneInfo.ConvertTimeFromUtc(tokenExpiration, TimeZoneInfo.Local);

            if (string.IsNullOrWhiteSpace(errorMessage))
            {
                var playFabSession = new PlayFabSession
                {
                    SessionTicket   = playFabResult.SessionTicket,
                    Email           = playFabResult.Email,
                    PlayFabId       = playFabResult.PlayFabId,
                    Budget          = playFabResult.Budget,
                    TokenExpiration = localTokenExpiration.ToString()
                };

                await SetSessionAsync(playFabSession, identity, authenticationProperties);
            }

            return(new AuthResponse
            {
                ErrorMessage = errorMessage,
                Email = playFabResult?.Email,
                PlayFabId = playFabResult?.PlayFabId,
                UserName = playFabResult?.DisplayName,
                NewlyCreated = newlyCreated,
            });
        }
Ejemplo n.º 2
0
        private async Task SetSessionAsync(PlayFabSession session, ClaimsIdentity identity = null, AuthenticationProperties authenticationProperties = null)
        {
            var claimsIdentity = identity ?? new ClaimsIdentity(
                new List <Claim>
            {
                new Claim(Helpers.Constants.Authentication.SchemaClaim, Helpers.Constants.Authentication.PlayFabSchema)
            },
                CookieAuthenticationDefaults.AuthenticationScheme
                );

            claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, "User"));

            if (!string.IsNullOrEmpty(session.Email))
            {
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, session.Email));
            }

            claimsIdentity.AddClaim(new Claim(PlayFabClaims.Budget, $"{config.Currency} {session.Budget}"));

            if (!string.IsNullOrEmpty(session.PlayFabId))
            {
                claimsIdentity.AddClaim(new Claim(PlayFabClaims.PlayFabId, session.PlayFabId));
            }

            var authProperties = authenticationProperties ?? new AuthenticationProperties();

            authProperties.IsPersistent = true;

            authProperties.StoreTokens(new List <AuthenticationToken> {
                new AuthenticationToken
                {
                    Name  = "SessionTicket",
                    Value = session.SessionTicket
                },
                new AuthenticationToken
                {
                    Name  = "TokenExpiration",
                    Value = session.TokenExpiration
                }
            });

            await httpContextAccessor.HttpContext.SignOutAsync();

            await httpContextAccessor.HttpContext.SignInAsync(
                new ClaimsPrincipal(claimsIdentity),
                authProperties);
        }
Ejemplo n.º 3
0
        private async void SetSession(PlayFabSession session)
        {
            var claimsIdentity = new ClaimsIdentity(
                new List <Claim>
            {
                new Claim(ClaimTypes.Role, "User"),
            },
                CookieAuthenticationDefaults.AuthenticationScheme
                );

            if (!string.IsNullOrEmpty(session.Email))
            {
                claimsIdentity.AddClaim(new Claim(ClaimTypes.Name, session.Email));
            }

            if (!string.IsNullOrEmpty(session.PlayFabId))
            {
                claimsIdentity.AddClaim(new Claim(PlayFabClaims.PlayFabId, session.PlayFabId));
            }

            claimsIdentity.AddClaim(new Claim("SessionTicket", session.SessionTicket));

            var authProperties = new AuthenticationProperties
            {
                IsPersistent = true,
            };

            authProperties.StoreTokens(new List <AuthenticationToken> {
                new AuthenticationToken
                {
                    Name  = "SessionTicket",
                    Value = session.SessionTicket
                },
                new AuthenticationToken
                {
                    Name  = "TokenExpiration",
                    Value = session.TokenExpiration
                }
            });

            await _httpContextAccessor.HttpContext.SignOutAsync(AzureADB2CDefaults.AuthenticationScheme);

            await _httpContextAccessor.HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties);
        }
Ejemplo n.º 4
0
        private AuthResponse GetAuthResponse(DateTime tokenExpiration, string email = null, string sessionTicket = null, string errorMessage = null, LoginResult loginResult = null)
        {
            var localTokenExpiration = TimeZoneInfo.ConvertTimeFromUtc(tokenExpiration, TimeZoneInfo.Local);

            if (string.IsNullOrWhiteSpace(errorMessage))
            {
                var playFabSession = new PlayFabSession
                {
                    SessionTicket   = sessionTicket,
                    Email           = email,
                    PlayFabId       = loginResult.InfoResultPayload.AccountInfo.PlayFabId,
                    TokenExpiration = localTokenExpiration.ToString()
                };
                SetSession(playFabSession);
            }

            return(new AuthResponse
            {
                ErrorMessage = errorMessage,
                AccountInfo = loginResult.InfoResultPayload.AccountInfo,
                NewlyCreated = loginResult.NewlyCreated
            });
        }