Created
May 14, 2020 04:22
-
-
Save JeremyLikness/e34eede71f31ab0dc1769584d2237301 to your computer and use it in GitHub Desktop.
Cosmos extensions for the server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Security.Claims; | |
using System.Threading.Tasks; | |
using IdentityServer4.Extensions; | |
using Microsoft.AspNetCore.Identity; | |
using BlazorCosmosWasm.Server.Models; | |
using Microsoft.Azure.Cosmos; | |
using Microsoft.Extensions.Configuration; | |
namespace BlazorCosmosWasm.Server.Controllers | |
{ | |
public static class CosmosExtensions | |
{ | |
public async static Task<string> GetEmailAsync(this ClaimsPrincipal user, UserManager<ApplicationUser> userManager) | |
{ | |
if (user.IsAuthenticated()) | |
{ | |
var id = user.Claims.SingleOrDefault( | |
key => key.Type == ClaimTypes.NameIdentifier)?.Value; | |
var appUser = await userManager.FindByIdAsync(id); | |
return appUser?.Email; | |
} | |
return null; | |
} | |
public static CosmosClient CreateCosmosClientFromConfig(this IConfiguration config) | |
{ | |
return new CosmosClient(config.GetValue<string>("CosmosConnection")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment