Пример #1
0
        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());
        }
Пример #2
0
        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));
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        /// <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);
        }
Пример #5
0
 /// <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());
 }