Beispiel #1
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginModel model, string returnUrl = null)
        {
            if (await Configurations.GetBooleanAsync("enable_register") == false)
            {
                return(ExternalRegisterClosed());
            }

            var check = new RegisterNotification(model.Username);
            await Mediator.Publish(check);

            if (check.Failed)
            {
                ModelState.AddModelError("xys::custom_rule", "The username is invalid. Please change another one.");
            }

            if (!ModelState.IsValid)
            {
                ViewData["ReturnUrl"] = returnUrl;
                return(View(nameof(ExternalLogin), model));
            }

            // Get the information about the user from the external login provider
            var info = await SignInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(Message(
                           title: "External login",
                           message: "Error loading external login information during confirmation.",
                           type: BootstrapColor.danger));
            }

            var user = UserManager.CreateEmpty(model.Username);

            user.Email        = model.Email;
            user.RegisterTime = DateTimeOffset.Now;
            var result = await UserManager.CreateAsync(user);

            if (result.Succeeded)
            {
                result = await UserManager.AddLoginAsync(user, info);
            }

            if (result.Succeeded)
            {
                await SignInManager.SignInAsync(user, isPersistent : false);

                await HttpContext.AuditAsync(
                    "registered",
                    user.Id.ToString(),
                    $"at {HttpContext.Connection.RemoteIpAddress} via {info.LoginProvider}");

                return(RedirectToLocal(returnUrl));
            }
            else
            {
                return(ErrorView(result, model, nameof(ExternalLogin)));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> Register(RegisterModel model, string returnUrl = null)
        {
            if (await Configurations.GetBooleanAsync("enable_register") == false)
            {
                return(RegisterClosed());
            }

            var check = new RegisterNotification(model.UserName);
            await Mediator.Publish(check);

            if (check.Failed)
            {
                ModelState.AddModelError("xys::custom_rule", "The username is invalid. Please change another one.");
            }

            ViewData["ReturnUrl"] = returnUrl;
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = UserManager.CreateEmpty(model.UserName);

            user.Email        = model.Email;
            user.RegisterTime = DateTimeOffset.Now;

            var result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(ErrorView(result, model));
            }

            if (user.Id == 1)
            {
                await UserManager.AddToRoleAsync(user, "Administrator");
            }

            var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.Action(
                action: "ConfirmEmail",
                controller: "Sign",
                values: new { userId = $"{user.Id}", code, area = "Account" },
                protocol: Request.Scheme);

            await EmailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

            await SignInManager.SignInAsync(user, isPersistent : false);

            await HttpContext.AuditAsync(
                "registered",
                user.Id.ToString(),
                $"at {HttpContext.Connection.RemoteIpAddress}");

            return(RedirectToLocal(returnUrl));
        }
Beispiel #3
0
        private static void RegisterNotificationRegistrationFunctions(ContainerBuilder builder)
        {
            builder.Register(
                c =>
            {
                var ctx = c.Resolve <IComponentContext>();
                RegisterNotification func = (map, subjects) =>
                {
                    var collection        = ctx.Resolve <INotificationCollection>();
                    var subjectCollection = ctx.Resolve <IRegisterSubjectGroups>();

                    collection.Register(map.Definitions);
                    foreach (var subject in subjects)
                    {
                        subjectCollection.RegisterNotificationForProvidedSubjectGroup(
                            subject.Subject,
                            map.NotificationType,
                            subject.Version,
                            subject.Group);
                    }
                };

                return(func);
            })
            .As <RegisterNotification>()
            .SingleInstance();

            builder.Register(
                c =>
            {
                var ctx = c.Resolve <IComponentContext>();
                RegisterRequiredNotification func = (type, subjects) =>
                {
                    type.VerifyThatTypeIsACorrectNotificationSet();

                    var subjectCollection = ctx.Resolve <IRegisterSubjectGroups>();
                    foreach (var subject in subjects)
                    {
                        subjectCollection.RegisterNotificationForRequiredSubjectGroup(subject.Subject, type, subject.Version, subject.Group);
                    }
                };

                return(func);
            })
            .As <RegisterRequiredNotification>()
            .SingleInstance();
        }