public async Task <Maybe <IIdentity> > HandleAuthenticationAsync(MiddlewareData data, IPrincipal principal) { var telegramIdentity = principal.GetIdentity <TelegramIdentity>() .OrElseThrow(() => new InvalidOperationException("TelegramIdentity is required")); var telegramUserId = telegramIdentity.Id; var filter = Builders <T> .Filter.Eq(u => u.UserId, telegramUserId); var newUser = await _identityFactory.CreateIdentityAsync(principal); var update = new BsonDocument { { "$setOnInsert", newUser.ToBsonDocument() } }; IIdentity identity = await GetDatabase().GetCollection <T>(CollectionNames.UsersCollection) .FindOneAndUpdateAsync( filter: filter, update: update, options: new FindOneAndUpdateOptions <T>() { IsUpsert = true, ReturnDocument = ReturnDocument.After } ); return(identity.NotNull()); }
public Task <IIdentity> CreateIdentityAsync(IPrincipal principal) { var telegramIdentity = principal.GetIdentity <TelegramIdentity>() .OrElseThrow(() => new InvalidOperationException("TelegramIdentity is required")); IIdentity user = ApplicationUser.Create(telegramIdentity); return(Task.FromResult(user)); }
/// <summary> /// Returns Role of given principal /// </summary> public static Role GetRole(this IPrincipal principal) { if (principal == null) { throw new ArgumentNullException("principal"); } return(principal.GetIdentity().Role); }
/// <summary> /// Gets the name for the user with the specified principal. /// </summary> /// <param name="principal">An IPrincipal that is the principal to get the name for.</param> /// <param name="removeDomainName">A bool that indicates whether to remove the domain name from the principal.</param> /// <returns>A string containing the name.</returns> public static string GetUserName(this IPrincipal principal, bool removeDomainName = true) { string returnValue; returnValue = principal.GetIdentity().Name; if (removeDomainName && returnValue.Contains(@"\")) { returnValue = returnValue.Remove(0, returnValue.IndexOf(@"\") + 1); } return(returnValue); }
/// <summary> /// Gets the user principal for the specified IPrincipal. /// </summary> /// <param name="principal">An IPrincipal to get the UserPrincipal for.</param> /// <returns>An UserPrincipal that is the user principal.</returns> public static UserPrincipal GetUserPrincipal(this IPrincipal principal) { return(principal.GetIdentity().GetUserPrincipal()); }