Exemplo n.º 1
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, AccountLinkingInfoViewModel accountLinkingModel, string returnUrl)
        {
            if (this.User.Identity.IsAuthenticated)
            {
                return this.RedirectToAction("Index", "Manage");
            }

            if (this.ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await this.AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return this.View("ExternalLoginFailure");
                }

                User user;
                IdentityResult result = null;
                bool isSuccessful = true;

                if (accountLinkingModel.AuskfId > 0)
                {
                    //Find user by auskf id if it is provided
                    using (var context = new DataContext())
                    {
                        user = (from x in context.Users.Include(u => u.Profile)
                                where x.AuskfId == accountLinkingModel.AuskfId
                                select x).FirstOrDefault();
                        user.UserName = model.Email;
                    }
                }
                else
                {
                    using (var context = new DataContext())
                    {
                        int auskfId = (from x in context.Users
                                       select x.AuskfId).Max();
                        // TODO This should not be created here, move to factory
                        user = new User {
                            Profile = new UserProfile(),
                            AuskfId = auskfId
                        };
                    }
                }

                user.UserName = model.Email;
                user.PasswordLastChangedDate = DateTime.Now;
                user.MaximumDaysBetweenPasswordChange = 180;
                user.Email = model.Email;
                user.JoinedDate = DateTime.Now; 
                user.LastLogin = DateTime.Now;
                user.Active = true;

                if (accountLinkingModel.AuskfId == 0)
                {
                    result = await this.UserManager.CreateAsync(user);
                    isSuccessful = result.Succeeded;
                }

                if (isSuccessful)
                {
                    result = await this.UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await this.SignInManager.SignInAsync(user, false, false);
                        return this.RedirectToLocal(returnUrl);
                    }
                }

                this.AddErrors(result);
            }

            this.ViewBag.ReturnUrl = returnUrl;
            return this.View(model);
        }
Exemplo n.º 2
0
        public async Task<ActionResult> Register(RegisterViewModel model, AccountLinkingInfoViewModel accountLinkingModel)
        {
            try
            {
                if (this.ModelState.IsValid)
                {
                    User user;
                    IdentityResult result;

                    if (accountLinkingModel.AuskfId > 0)
                    {
                        using (var context = new DataContext())
                        {
                            user = (from x in context.Users.Include(u => u.Profile)
                                    where x.AuskfId == accountLinkingModel.AuskfId
                                    select x).FirstOrDefault();
                        }
                        user.UserName = model.Email;
                        user.Email = model.Email;
                        user.Password = model.Password;
                        result = await this.UserManager.UpdateAsync(user);
                    }
                    else
                    {
                        user = new User
                        {
                            UserName = model.Email,
                            Email = model.Email
                        };
                        result = await this.UserManager.CreateAsync(user, model.Password);
                    }

                    if (result.Succeeded)
                    {
                        var code = await this.UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        var url = this.Request.Url;
                        if (url != null)
                        {
                            var callbackUrl = this.Url.Action("ConfirmEmail", "Account", new
                            {
                                userId = user.Id,
                                code
                            }, url.Scheme);

                            await this.UserManager.SendEmailAsync(user.Id, "Confirm your account",
                                    "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                            this.ViewBag.Link = callbackUrl;
                        }
                        return this.View("DisplayEmail");
                    }

                    this.AddErrors(result);
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"",
                            ve.PropertyName,
                            eve.Entry.CurrentValues.GetValue<object>(ve.PropertyName),
                            ve.ErrorMessage);
                    }
                }
                throw;
            }


            // If we got this far, something failed, redisplay form
            return this.View(model);
        }