コード例 #1
0
        public async Task<JsonResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = await UserManager.FindByEmailAsync(model.Email);
                if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return Json(new { success = false, msg = "Check your email to reset your password." });
                }

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
                var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);

                IdentityMessage myMessage = new IdentityMessage();
                myMessage.Destination = user.Email;
                myMessage.Subject = "VGWager Reset Password";

                myMessage.Body = "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>";
                // Create credentials, specifying your user name and password.
                //var credentials = newNetworkCredential("username", "password");
                //// Create a REST transport for sending email.
                //var transportREST = Web.GetInstance(credentials);
                //// Send the email.
                //transportREST.Deliver(myMessage);

                VGWagers.Utilities.EmailService emailServ = new VGWagers.Utilities.EmailService();

                await emailServ.SendAsync(myMessage);

               // await UserManager.SendEmailAsync(user.Id, "Reset Password", "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>");

                return Json(new { success = false, msg = "Check your email to reset your password." });
            }

            // If we got this far, something failed, redisplay form
            return Json(new { success = false, msg = "Server Error" });
        }
コード例 #2
0
        public async Task<JsonResult> RegisterJson(RegisterViewModel model, string returnUrl)
        {
          try { 
            
                if (! ModelState.IsValid)
                {  
                        var errors = GetErrorsFromModelState();
                        return Json(new { success = false, errors = errors });
                }
                else
                {
                    //switch (result)
                    //{

                    //    case SignInStatus.Success:
                    //        return Json(true);
                    //    case SignInStatus.LockedOut:
                    //    case SignInStatus.RequiresVerification:
                    //    case SignInStatus.Failure:
                    //        return Json(false);
                    //    default:
                    //        break;
                    //}
                    //return Json(false);
                    var user = new ApplicationUser { UserName = model.Username, Email = model.Email, DateOfBirth = model.DateOfBirth , TermsAndConditionAccepted = model.TermsAccepted, MarketingMailersAccepted = model.MarketingMailersAccepted };
                   
                  
                    var tempUser = await UserManager.FindByEmailAsync(model.Email);
                   
                    var result = (IdentityResult)null;

                    if (tempUser != null)
                    {
                        return Json(new { success = false, msg = "User already exists" });
                    }
                    else
                    {
                      result = await UserManager.CreateAsync(user, model.Password);
                    }   
                    if (result.Succeeded)
                    {

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

                        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                        // Send an email with this link
                        // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        var callbackUrl = Url.Action("ConfirmEmail", "Account",
                           new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);


                        IdentityMessage myMessage = new IdentityMessage();
                        myMessage.Destination = user.Email;
                        myMessage.Subject = "VGWager Account Confirmation";
                        
                        myMessage.Body = "Please confirm your account by clicking <a href=\""  + callbackUrl + "\">here</a>";
                        // Create credentials, specifying your user name and password.
                        //var credentials = newNetworkCredential("username", "password");
                        //// Create a REST transport for sending email.
                        //var transportREST = Web.GetInstance(credentials);
                        //// Send the email.
                        //transportREST.Deliver(myMessage);

                        VGWagers.Utilities.EmailService emailServ = new VGWagers.Utilities.EmailService();

                        await emailServ.SendAsync(myMessage);

                        //await UserManager.SendEmailAsync(user.Id,
                        //   "Confirm your account", "Please confirm your account by clicking <a href=\""
                        //   + callbackUrl + "\">here</a>");

                        Success("Check your email and confirm your account, you must be confirmed before you can log in.", true);
                        return Json(new { success = true, msg = "Check your email and confirm your account, you must be confirmed before you can log in." });
                    }
                    else
                    { 
                        return Json(new { success = false, msg = "Username and/or Email has been taken." }); 
                    }
                    
                }

            }
            catch(Exception ex)
            {
                return Json(new { success = false, msg = ex.InnerException.Message});
            }

                // If we got this far, something failed, redisplay form
                //return Json(new { success = false, msg = "Server Error" });
        }