public async Task <IActionResult> MoodleLoginAsync() { try { // Parse and check request var authData = await MoodleAuthenticationTools.ParseAuthenticationRequestAsync(Request, _ltiOptions.OAuthConsumerKey, _ltiOptions.OAuthSharedSecret); // Create identity var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); identity.AddClaim(new Claim(ClaimTypes.Name, authData.LoginName)); identity.AddClaim(new Claim(ClaimTypes.Email, authData.Email)); // TODO role // Sign in await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity)); // TODO return(Json(authData)); } catch { // TODO throw; } }
public async Task <IActionResult> LoginMoodleAsync([FromServices] IOptions <MoodleLtiOptions> moodleLtiOptions) { // Already logged in? var currentUser = await GetCurrentUserAsync(); if (currentUser != null) { return(await RenderAsync(ViewType.Redirect)); } // Parse and check request MoodleAuthenticationMessageData authData; try { authData = await MoodleAuthenticationTools.ParseAuthenticationRequestAsync ( Request, moodleLtiOptions.Value.OAuthConsumerKey, moodleLtiOptions.Value.OAuthSharedSecret, _serviceProvider.GetRequiredService <ILogger <MoodleAuthenticationTools> >() ); } catch (SecurityException) { AddStatusMessage(_localizer["LoginMoodleAsync:InvalidLogin"], StatusMessageTypes.Error); return(await RenderAsync(ViewType.Blank)); } // Does the user exist already? var user = await _userService.FindUserByMoodleUserIdAsync(authData.UserId, HttpContext.RequestAborted); if (user == null) { bool firstUser = !await _userService.AnyUsers(HttpContext.RequestAborted); var newUser = new User { DisplayName = authData.FullName, MoodleUserId = authData.UserId, MoodleName = authData.LoginName, GroupFindingCode = RandomStringGenerator.GetRandomString(10), IsAdmin = firstUser }; user = await _userService.CreateUserAsync(newUser, HttpContext.RequestAborted); AddStatusMessage(_localizer["LoginMoodleAsync:AccountCreationSuccess"], StatusMessageTypes.Success); } // Sign in user await DoLoginAsync(user); // Done AddStatusMessage(_localizer["LoginMoodleAsync:Success"], StatusMessageTypes.Success); if (user.Group == null) { return(await ShowGroupFormAsync()); } return(await RenderAsync(ViewType.Redirect)); }