コード例 #1
0
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid == false)
            {
                return(View(model));
            }

            var tenant = await _domainContext.Tenants.Get(model.Name);

            if (tenant != null)
            {
                ModelState.AddModelError("Name", "Name is already taken.");
                return(View(model));
            }

            using (var scope = await _domainContext.BeginTransaction())
            {
                tenant    = new Tenant();
                tenant.Id = model.Name;
                _domainContext.Tenants.Save(tenant);

                ApplicationUser user = new ApplicationUser();
                user.UserName           = model.Email;
                user.NormalizedUserName = model.Email.ToUpper();
                user.Email           = model.Email;
                user.NormalizedEmail = model.Email.ToUpper();
                user.PasswordHash    = _passwordHasher.HashPassword(user, model.Password);
                user.SecurityStamp   = Guid.NewGuid().ToString();
                _domainContext.Users.Save(user, model.Name);
                await _domainContext.SaveAsync();

                scope.Commit();
            }

            StringBuilder body = new StringBuilder();

            body.Append("Thank you for registering.<br/><br/>");
            body.AppendFormat("The url of your console is <a href=\"{0}\">{0}</a>.<br/>", _urlService.GetUrlWithTenant(model.Name));
            await _emailService.SendEmailAsync(
                model.Email,
                "Thank you for registering",
                body.ToString());

            return(this.RedirectToAction("RegistrationConfirmation", new { tenant = model.Name }));
        }