예제 #1
0
 public async Task<IActionResult> SendConfirmationEmail(SendConfirmationEmailAddressModel model)//User Id
 {
     var user = await _grantChecker.EnsureGranted(model.AccessToken, model.OpenId, t => t.ConfirmEmail);
     var useremail = await _dbContext.UserEmails.SingleOrDefaultAsync(t => t.EmailAddress == model.Email.ToLower());
     if (useremail == null)
     {
         return this.Protocol(ErrorType.NotFound, $"Can not find your email:{model.Email}");
     }
     if (useremail.OwnerId != user.Id)
     {
         return this.Protocol(ErrorType.Unauthorized, $"The account you tried to authorize is not an account with id: {model.OpenId}");
     }
     if (useremail.Validated)
     {
         return this.Protocol(ErrorType.HasDoneAlready, $"The email: {model.Email} was already validated!");
     }
     // limit the sending frenquency to 3 minutes.
     if (DateTime.UtcNow > useremail.LastSendTime + new TimeSpan(0, 1, 0))
     {
         var token = Guid.NewGuid().ToString("N");
         useremail.ValidateToken = token;
         useremail.LastSendTime = DateTime.UtcNow;
         await _dbContext.SaveChangesAsync();
         try
         {
             await _emailSender.SendConfirmation(user.Id, useremail.EmailAddress, token);
         }
         catch (SmtpException e)
         {
             return this.Protocol(ErrorType.InvalidInput, e.Message);
         }
         return this.Protocol(ErrorType.Success, "Successfully sent the validation email.");
     }
     return this.Protocol(ErrorType.RequireAttention, "We have just sent you an Email in an minute.");
 }
예제 #2
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (!_captcha.ValidateCaptchaCode(model.CaptchaCode, HttpContext.Session))
            {
                ModelState.AddModelError(string.Empty, "Invalid captacha code!");
            }
            var app = (await _apiService.AppInfoAsync(model.AppId)).App;

            if (!ModelState.IsValid)
            {
                model.Recover(app.AppName, app.IconPath);
                return(View(model));
            }
            bool exists = _dbContext.UserEmails.Any(t => t.EmailAddress == model.Email.ToLower());

            if (exists)
            {
                ModelState.AddModelError(string.Empty, $"An user with email '{model.Email}' already exists!");
                model.Recover(app.AppName, app.IconPath);
                return(View(model));
            }
            var user = new GatewayUser
            {
                UserName          = model.Email,
                Email             = model.Email,
                NickName          = model.Email.Split('@')[0],
                PreferedLanguage  = model.PreferedLanguage,
                IconFilePath      = Values.DefaultImagePath,
                RegisterIPAddress = HttpContext.Connection.RemoteIpAddress.ToString()
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var primaryMail = new UserEmail
                {
                    EmailAddress  = model.Email.ToLower(),
                    OwnerId       = user.Id,
                    ValidateToken = Guid.NewGuid().ToString("N")
                };
                _dbContext.UserEmails.Add(primaryMail);
                await _dbContext.SaveChangesAsync();

                // Send him an confirmation email here:
                await _emailSender.SendConfirmation(user.Id, primaryMail.EmailAddress, primaryMail.ValidateToken);

                await _authLogger.LogAuthRecord(user.Id, HttpContext.Connection.RemoteIpAddress.ToString(), true, app.AppId);

                await _signInManager.SignInAsync(user, isPersistent : true);

                return(await _authManager.FinishAuth(user, model, app.ForceConfirmation));
            }
            AddErrors(result);
            model.Recover(app.AppName, app.IconPath);
            return(View(model));
        }
예제 #3
0
        public async Task <IActionResult> AppRegister(AppRegisterAddressModel model)
        {
            var  appId  = _tokenManager.ValidateAccessToken(model.AccessToken);
            bool exists = _dbContext.UserEmails.Any(t => t.EmailAddress == model.Email.ToLower());

            if (exists)
            {
                return(this.Protocol(ErrorType.NotEnoughResources, $"A user with email '{model.Email}' already exists!"));
            }
            var user = new APIUser
            {
                UserName         = model.Email,
                Email            = model.Email,
                NickName         = model.Email.Split('@')[0],
                PreferedLanguage = "en",
                IconFilePath     = Values.DefaultImagePath
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var primaryMail = new UserEmail
                {
                    EmailAddress  = model.Email.ToLower(),
                    OwnerId       = user.Id,
                    ValidateToken = Guid.NewGuid().ToString("N")
                };
                _dbContext.UserEmails.Add(primaryMail);
                await _dbContext.SaveChangesAsync();

                // Send him an confirmation email here:
                try
                {
                    await _emailSender.SendConfirmation(user.Id, primaryMail.EmailAddress, primaryMail.ValidateToken);
                }
                // Ignore smtp exception.
                catch (SmtpException) { }
                // Grant this app.
                if (!await user.HasAuthorizedApp(_dbContext, appId))
                {
                    await user.GrantTargetApp(_dbContext, appId);
                }
                return(this.Protocol(ErrorType.Success, "Successfully created your account."));
            }
            return(this.Protocol(ErrorType.NotEnoughResources, result.Errors.First().Description));
        }
예제 #4
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            App app;

            try
            {
                app = (await _apiService.AppInfoAsync(model.AppId)).App;
            }
            catch (AiurUnexceptedResponse)
            {
                return(NotFound());
            }
            if (!ModelState.IsValid)
            {
                model.Recover(app.AppName, app.AppIconAddress);
                return(View(model));
            }
            bool exists = _dbContext.UserEmails.Any(t => t.EmailAddress == model.Email.ToLower());

            if (exists)
            {
                ModelState.AddModelError(string.Empty, $"An user with email '{model.Email}' already exists!");
                model.Recover(app.AppName, app.AppIconAddress);
                return(View(model));
            }
            var user = new APIUser
            {
                UserName         = model.Email,
                Email            = model.Email,
                NickName         = model.Email.Split('@')[0],
                PreferedLanguage = model.PreferedLanguage,
                HeadImgFileKey   = Values.DefaultImageId
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                var primaryMail = new UserEmail
                {
                    EmailAddress  = model.Email.ToLower(),
                    OwnerId       = user.Id,
                    ValidateToken = Guid.NewGuid().ToString("N")
                };
                _dbContext.UserEmails.Add(primaryMail);
                await _dbContext.SaveChangesAsync();

                // Send him an confirmation email here:
                try
                {
                    await _emailSender.SendConfirmation(user.Id, primaryMail.EmailAddress, primaryMail.ValidateToken);
                }
                // Ignore smtp exception.
                catch (SmtpException) { }
                await _signInManager.SignInAsync(user, isPersistent : true);

                return(await FinishAuth(model));
            }
            AddErrors(result);
            model.Recover(app.AppName, app.AppIconAddress);
            return(View(model));
        }