public async Task <IActionResult> Accept(OpenIdConnectRequest request) { // Retrieve the profile of the logged in user. var user = await _userManager.GetUserAsync(User); if (user == null) { return(View("Error", new ErrorViewModel { Error = OpenIdConnectConstants.Errors.ServerError, ErrorDescription = "An internal error has occurred" })); } // Create a new ClaimsIdentity containing the claims that // will be used to create an id_token, a token or a code. var identity = await _userManager.CreateIdentityAsync(user, request.GetScopes()); // Create a new authentication ticket holding the user identity. var ticket = new AuthenticationTicket( new ClaimsPrincipal(identity), new AuthenticationProperties(), OpenIdConnectServerDefaults.AuthenticationScheme); ticket.SetResources(request.GetResources()); ticket.SetScopes(request.GetScopes()); // Returning a SignInResult will ask OpenIddict to issue the appropriate access/identity tokens. return(SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme)); }
public async Task <IActionResult> Get() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(Ok("No user / not logged in")); // if Authorize is not applied } var claims = User.Claims.Select(claim => new { claim.Type, claim.Value }).ToArray(); return(Json(claims)); }
/// <summary>All user data including Person that has been updated after a specified date.</summary> /// <param name="startUnixUtcTimeSeconds">The earliest updated time to include, in UTC seconds since Unix epoch.</param> /// <returns>All user data apart from SensorHistory; people, locations, devies, cropCycles and sensors. Also sends /// serverDateTime just before the queries to get the data are made.</returns> public async Task <IActionResult> GetUserData(long?startUnixUtcTimeSeconds) { // Get the current server time before any database requests are started. // The client will regard itself be up-to-date to this time. var serverDateTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); var startDateTime = DateTimeOffset.FromUnixTimeSeconds(startUnixUtcTimeSeconds ?? 0); var user = await _userManager.GetUserAsync(User); var userGuid = new Guid(user.Id); // This will be an array with either 1 or 0 entries. This is more conventient than not using an array. var people = await _db.People .Where(p => p.Id == userGuid) .Where(p => p.UpdatedAt >= startDateTime).ToArrayAsync(); var locations = await _db.Locations .Where(l => l.PersonId == userGuid) .Where(l => l.UpdatedAt >= startDateTime).ToArrayAsync(); var locationIds = locations.Select(l => l.Id); var devices = await _db.Devices .Where(d => locationIds.Contains(d.LocationId)) .Where(d => d.UpdatedAt >= startDateTime).ToArrayAsync(); var cropcycles = await _db.CropCycles .Where(c => locationIds.Contains(c.LocationId)) .Where(c => c.UpdatedAt >= startDateTime).ToArrayAsync(); var cropCycleIds = cropcycles.Select(c => c.Id); var sensors = await _db.Sensors .Where(s => cropCycleIds.Contains(s.Id)) .Where(s => s.UpdatedAt >= startDateTime).ToArrayAsync(); var userData = new Poco.User.SyncData(serverDateTime, people, locations, devices, cropcycles, sensors); return(new JsonResult(userData)); }
private async Task <ApplicationUser> GetCurrentUserAsync() { return(await _userManager.GetUserAsync(User)); }