Exemplo n.º 1
0
 private async Task SignInAsync(AppUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
Exemplo n.º 2
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new AppUser() { UserName = model.UserName, IsActive = null };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    //await SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("Index", "Home");
                    
                    //Send Email to the Applicant and Then Save to Database
                    try
                    {
                        var fromAddress = new MailAddress("*****@*****.**", "Compro Admission");
                        const string fromPassword = "******";

                        var toAddress = new MailAddress(model.UserName);
                        const string subject = "Registration Confirmation, CAS, MUM";

                        string body = string.Format("User Name: {0}{1}Password: {2}{1}{1}Click on the following link to active your account-{1}  {3}://{4}/Account/Active?Applicant={5}", model.UserName, Environment.NewLine, model.Password, Request.Url.Scheme, Request.Url.Authority, user.Id);

                        var smtp = new SmtpClient
                        {
                            Host = "smtp.gmail.com",
                            Port = 587,
                            EnableSsl = true,
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
                        };
                        using (var message = new MailMessage(fromAddress, toAddress)
                        {
                            Subject = subject,
                            Body = body
                        })
                        {
                            smtp.Send(message);
                        }
                    }
                    catch (Exception ex)
                    {
                        //Delete user
                        db.Users.Remove(db.Users.Find(user.Id));
                        db.SaveChanges();

                        //Invalid Email
                        ViewBag.Message = ex.Message + "<br />Sorry, we are unable to send you mail.<br />Please check your email address.";
                        return View();
                    }
                    return View("RegisterThankYou");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemplo n.º 3
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new AppUser() { UserName = model.UserName };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }