public async Task <IActionResult> RegisterTenant(RegisterTenantViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            if (model.BetaAccessKey != _betaAccessHelper.BetaAccessKey)
            {
                return(UnsuccessfulTenantCreation(CreateTenantResult.InvalidBetaAccessKey));
            }

            var tenantProvider = HttpContext.GetService <TenantProvider>();
            var result         = await tenantProvider.CreateTenantAsync(new CreateTenantParameters
            {
                OrganizationName   = model.OrganizationName,
                OrganizationDomain = model.OrganizationDomain
            });

            if (result != CreateTenantResult.TenantCreated)
            {
                return(UnsuccessfulTenantCreation(result));
            }
            UserManager.CreateAccount(model.OrganizationDomain, model.Email, model.Password, model.Email, null, null, null, new List <Claim>
            {
                new Claim(AuthorizationPolicies.AppOwner, "true")
            });

            return(TenantRedirectHelper.RedirectToTenantDomain(model.OrganizationDomain, "Login", HttpContext.Request, Url));
        }
        public IActionResult ActivateAccount(ActivateAccountViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(string.IsNullOrWhiteSpace(model.VerificationCode)
                    ? View("Error")
                    : View(model));
            }

            var user = UserManager.GetByVerificationKey(model.VerificationCode);

            if (user == null)
            {
                return(View("Error"));
            }

            if (user.IsAccountVerified)
            {
                return(User.Identity.IsAuthenticated
                    ? TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Index", "Home", Request, Url)
                    : TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Login", Request, Url));
            }

            UserManager.VerifyEmailFromKey(model.VerificationCode, model.Password, out user);
            SignInManager.SignIn(user);

            return(TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Index", "Home", Request, Url));
        }
        public IActionResult RegisterTenant()
        {
            if (!string.IsNullOrWhiteSpace(Tenant))
            {
                return(TenantRedirectHelper.RedirectToRootDomain(RouteData.Values, Request, Url));
            }

            return(View());
        }
        public IActionResult ActivateAccount(string code)
        {
            var user = UserManager.GetByVerificationKey(code);

            if (user.IsAccountVerified)
            {
                return(User.Identity.IsAuthenticated
                    ? TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Index", "Home", Request, Url)
                    : TenantRedirectHelper.RedirectToTenantDomain(user.Tenant, "Login", Request, Url));
            }

            return(View(new ActivateAccountViewModel
            {
                VerificationCode = code,
                DisplayPasswordConfirmation = !user.HasPassword()
            }));
        }
        public async Task <IActionResult> LoginTenant(TenantLoginViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                ViewData["ReturnUrl"] = returnUrl;
                return(View("LoginTenant"));
            }

            var tenantProvider = HttpContext.GetService <TenantProvider>();
            var tenantExists   = await tenantProvider.LookupTenantByDomainAsync(model.TenantDomain);

            if (!tenantExists)
            {
                ModelState.AddModelError("", "We couldn't find your organization.");
                return(Login());
            }

            return(TenantRedirectHelper.RedirectToTenantDomain(model.TenantDomain, "Login", HttpContext.Request, Url));
        }