Skip to content

Instantly share code, notes, and snippets.

@JeremyLikness
Created May 14, 2020 04:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JeremyLikness/e34eede71f31ab0dc1769584d2237301 to your computer and use it in GitHub Desktop.
Save JeremyLikness/e34eede71f31ab0dc1769584d2237301 to your computer and use it in GitHub Desktop.
Cosmos extensions for the server
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