public async Task <IActionResult> OauthCallback(string code, string state = null) { var siteDomain = _configuration.GetValue <string>("STACKAPP_SITEDOMAIN"); var accessToken = await _seApiService.GetAccessTokenFromCodeAsync(code, GetOauthReturnUrl()); var currentUser = await _seApiService.GetMyUserAsync(siteDomain, accessToken); if (currentUser == null) { return(Content("Could not retrieve a user account on " + siteDomain)); } var minRep = _configuration.GetValue <int>("MIN_REP_TO_LOGIN"); if (currentUser.Reputation < minRep) { return(Content($"You need at least {minRep} to log in")); } await _userService.UpsertUserAsync(new User { Id = currentUser.UserId, DisplayName = currentUser.DisplayName, IsModerator = currentUser.UserType == "moderator", CreationDate = DateTime.UtcNow, LastSeenDate = DateTime.UtcNow }); var user = await _userService.GetUserAsync(currentUser.UserId); var claims = new List <Claim> { new Claim(ClaimType.Id, user.Id.ToString(CultureInfo.InvariantCulture)), new Claim(ClaimType.Name, user.DisplayName), new Claim(ClaimType.UserType, user.UserType.ToString()) }; if (user.UserType >= UserType.User) { claims.Add(new Claim(ClaimType.CanSuggest, "1")); if (user.UserType >= UserType.TrustedUser) { claims.Add(new Claim(ClaimType.CanReview, "1")); } } if (user.IsModerator) { claims.Add(new Claim(ClaimType.IsModerator, "1")); } var identity = new ClaimsIdentity(claims, "login"); await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); return(Redirect(state ?? "/")); }
public async Task <IActionResult> OauthCallback(string code, string state = null) { string returnUrl = state; var siteDomain = _configuration.GetValue <string>("STACKAPP_SITEDOMAIN"); var accessToken = await _seApiService.GetAccessTokenFromCodeAsync(code, GetOauthReturnUrl()); var currentUser = await _seApiService.GetMyUserAsync(siteDomain, accessToken); if (currentUser == null) { return(Content("Could not retrieve a user account on " + siteDomain)); } var minRep = _configuration.GetValue <int>("MIN_REP_TO_LOGIN"); if (currentUser.Reputation < minRep) { return(Content($"You need at least {minRep} to log in")); } await _userService.UpsertUserAsync(new User { Id = currentUser.UserId, DisplayName = currentUser.DisplayName, IsModerator = currentUser.UserType == "moderator", CreationDate = DateTime.UtcNow, LastSeenDate = DateTime.UtcNow }); await LoginUser(currentUser.UserId); return(Redirect(returnUrl ?? "/")); }