Пример #1
0
        public JsonResult <ApiBaseResponse> LoginByPhone([FromUri] string phoneNumber)
        {
            var result = new ApiBaseResponse();

            try
            {
                var user = Mapper.Map <OntextUser, ApiUser>(UserManager.FindByPhoneNumber(phoneNumber));

                if (user == null)
                {
                    var userEntity = new OntextUser();

                    var phone = ServicesHost.GetService <IPhonesProvider>().GetByPhoneNumber(phoneNumber) ?? new ApiPhone
                    {
                        Number = phoneNumber
                    };

                    userEntity.UserName         = Guid.NewGuid().ToString();
                    userEntity.TwoFactorEnabled = true;

                    var userPassword = OntextSettings.UserDefaultPassword;

                    var r = UserManager.Create(userEntity, userPassword);

                    if (!r.Succeeded)
                    {
                        result.Status = Core.Enums.ApiStatusCode.WrongArgumentsOrData;
                        return(Json(result));
                    }

                    phone.UserId = userEntity.Id;
                    ServicesHost.GetService <IPhonesProvider>().Save(phone);

                    user = Mapper.Map <OntextUser, ApiUser>(UserManager.FindByPhoneNumber(phoneNumber));
                }

                var purpose = IdentityUserTokenHelper.GenerateTokenPurpose(IdentityUserTokenHelper.TokenPurpose.Loging, phoneNumber);
                var token   = UserManager.GenerateUserToken(purpose, user.Id);
#if DEBUG
                UserManager.SendEmail(ConfigurationManager.AppSettings["Email"], "Security Code", token);
#else
                UserManager.SendSms(phoneNumber, token);
#endif
            }
            catch (Exception ex)
            {
                result.Status = Core.Enums.ApiStatusCode.SystemError;
                result.Error  = ex.Message;
            }

            return(Json(result));
        }
Пример #2
0
        public bool Update(OntextUser user)
        {
            var oldUser = GetById(user.Id);

            if (oldUser == null)
            {
                return(false);
            }

            oldUser.Email = user.Email;

            return(Context.SaveChanges() > 0);
        }
Пример #3
0
        public bool Add(OntextUser user)
        {
            if (!user.Phones.Any())
            {
                throw new Exception("Can not create an user without a phone.");
            }

            if (user.Phones.Any(p => p.User != null))
            {
                throw new Exception("Can not create an user. One of the phones belongs to another user.");
            }

            Context.Users.Add(user);

            return(Context.SaveChanges() > 0);
        }
Пример #4
0
//        public ApiUser GetById(Guid id)
//        {
//            var user = _userManager.FindById(id);
//
//            return Mapper.Map<OntextUser, ApiUser>(user);
//        }

//        public ApiUser GetByPhoneNumber(string phoneNumber)
//        {
//            var user = _userManager.FindByPhoneNumber(phoneNumber);
//
//            return Mapper.Map<OntextUser, ApiUser>(user);
//        }

//        public ApiUser GetByEmailAddress(string emailAddress)
//        {
//            var user = _userManager.FindByEmail(emailAddress);
//
//            return Mapper.Map<OntextUser, ApiUser>(user);
//        }

//        public void SendTwoFactorTokenBySms(Guid userId, string phoneNumber)
//        {
//            var token = _userManager.GenerateTwoFactorToken(userId, OntextSettings.PhoneTwoFactorProvider);
//
//            _userManager.SendSms(phoneNumber, token);
//        }
//
//        public void SendTwoFactorTokenByEmail(Guid userId, string email)
//        {
//            var token = _userManager.GenerateTwoFactorToken(userId, OntextSettings.PhoneTwoFactorProvider);
//
//            _userManager.SendEmail(email, "Security Code", token);
//        }

//        public bool VerifyTwoFactorToken(Guid userId, string token)
//        {
//            var provider = OntextSettings.PhoneTwoFactorProvider;
//
//            return _userManager.VerifyTwoFactorToken(userId, provider, token);
//        }

        public bool Create(string phoneNumber)
        {
            var userEntity = new OntextUser();

            var phone = _phoneRepository.GetAll().FirstOrDefault(p => p.Number == phoneNumber) ?? new Phone
            {
                Number = phoneNumber
            };

            userEntity.UserName         = Guid.NewGuid().ToString();
            userEntity.Phones           = new[] { phone };
            userEntity.TwoFactorEnabled = true;

            var userPassword = OntextSettings.UserDefaultPassword;

            var result = _userManager.Create(userEntity, userPassword);

            return(result.Succeeded);
        }