public async Task <IActionResult> OnPostConfirmationAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); // Get the information about the user from the external login provider var info = await signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information during confirmation."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } if (ModelState.IsValid) { var user = new Player { UserName = Input.Username, Email = Input.Email }; var result = await userManager.CreateAsync(user); if (result.Succeeded) { result = await userManager.AddLoginAsync(user, info); if (result.Succeeded) { if (info.ProviderDisplayName == STEAM_PROVIDER_NAME) { var steamId64 = ulong.Parse(info.ProviderKey.Split('/').Last()); var steamUser = SteamApiHelper.GetSteamUserInstance(this.configuration); var playerResult = await steamUser.GetCommunityProfileAsync(steamId64); await this.userManager.AddClaimAsync(user, new Claim(STEAM_ID_64_CLAIM_TYPE, steamId64.ToString())); await this.userManager.AddClaimAsync(user, new Claim(PROFILE_AVATAR_CLAIM_TYPE, playerResult.AvatarFull.ToString())); await this.userManager.AddClaimAsync(user, new Claim(PROFILE_AVATAR_MEDIUM_CLAIM_TYPE, playerResult.AvatarMedium.ToString())); } await signInManager.SignInAsync(user, isPersistent : true); logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } LoginProvider = info.LoginProvider; ReturnUrl = returnUrl; return(Page()); }
public async Task <IActionResult> OnGetLinkLoginCallbackAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var info = await _signInManager.GetExternalLoginInfoAsync(await _userManager.GetUserIdAsync(user)); if (info == null) { throw new InvalidOperationException($"Unexpected error occurred loading external login info for user with ID '{user.Id}'."); } var result = await _userManager.AddLoginAsync(user, info); if (!result.Succeeded) { throw new InvalidOperationException($"Unexpected error occurred adding external login for user with ID '{user.Id}'."); } // Clear the existing external cookie to ensure a clean login process await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); StatusMessage = "The external login was added."; if (info.ProviderDisplayName == STEAM_PROVIDER_NAME) { var steamId64 = ulong.Parse(info.ProviderKey.Split('/').Last()); var steamUser = SteamApiHelper.GetSteamUserInstance(this._configuration); var playerResult = await steamUser.GetCommunityProfileAsync(steamId64); var userClaims = await this._userManager.GetClaimsAsync(user); await this._userManager.AddClaimAsync(user, new Claim(STEAM_ID_64_CLAIM_TYPE, steamId64.ToString())); await this._userManager.RemoveClaimAsync(user, new Claim(PROFILE_AVATAR_MEDIUM_CLAIM_TYPE, DEFAULT_AVATAR_MEDIUM)); await this._userManager.RemoveClaimAsync(user, new Claim(PROFILE_AVATAR_CLAIM_TYPE, DEFAULT_AVATAR)); await this._userManager.AddClaimAsync(user, new Claim(PROFILE_AVATAR_CLAIM_TYPE, playerResult.AvatarFull.ToString())); await this._userManager.AddClaimAsync(user, new Claim(PROFILE_AVATAR_MEDIUM_CLAIM_TYPE, playerResult.AvatarMedium.ToString())); } return(RedirectToPage()); }
public async Task <IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (remoteError != null) { ErrorMessage = $"Error from external provider: {remoteError}"; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } var info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { ErrorMessage = "Error loading external login information."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } // Sign in the user with this external login provider if the user already has a login. #region Custom var userLogin = this._playersRepository .AllAsNoTrackingWithDeleted() .Include(x => x.Logins) .SelectMany(x => x.Logins) .Where(x => x.ProviderKey == info.ProviderKey) .SingleOrDefault(); if (userLogin != null) { var isPlayerDeleted = (await this._playersRepository .GetByIdWithDeletedAsync(userLogin.UserId)) .IsDeleted; if (isPlayerDeleted) { ErrorMessage = $"Account associated with this steam account is deleted. Contact an administrator for restoring."; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl, ErrorMessage = ErrorMessage })); } } #endregion var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent : false, bypassTwoFactor : true); if (result.Succeeded) { _logger.LogInformation("{Name} logged in with {LoginProvider} provider.", info.Principal.Identity.Name, info.LoginProvider); return(LocalRedirect(returnUrl)); } if (result.IsLockedOut) { return(RedirectToPage("./Lockout")); } else { // If the user does not have an account, then ask the user to create an account. var steamId64 = ulong.Parse(info.ProviderKey.Split('/').Last()); var steamUser = SteamApiHelper.GetSteamUserInstance(this._configuration); var playerResult = await steamUser.GetCommunityProfileAsync(steamId64); if (playerResult.IsVacBanned) { ErrorMessage = $"VAC banned accounts cannot register!"; return(RedirectToPage("./Login", new { ReturnUrl = returnUrl })); } ReturnUrl = returnUrl; LoginProvider = info.LoginProvider; if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email)) { Input = new InputModel { Email = info.Principal.FindFirstValue(ClaimTypes.Email) }; } return(Page()); } }
public CheckForVACBans(IDeletableEntityRepository <Player> playerRepository, IConfiguration configuration) { this.playerRepository = playerRepository; this.mainSteamUserInstance = SteamApiHelper.GetSteamUserInstance(configuration); }