예제 #1
0
		public async Task<IHttpActionResult> Register(RegisterBindingModel model)
		{
			if (!ModelState.IsValid)
			{
				return BadRequest(ModelState);
			}

			UserProfile user = new UserProfile
			{
				UserName = model.UserName,
				Email = model.Email,
				EmailConfirmed = false
			};

			IdentityResult identityResult = await UserManager.CreateAsync(user, model.Password);

			IHttpActionResult createResult = GetErrorResult(identityResult);

			if (createResult != null)
			{
				return createResult;
			}

			UserProfile justCreatedUser = await UserManager.FindByNameAsync(model.UserName);

			IdentityResult roleResult = await UserManager.AddToRoleAsync(justCreatedUser.Id, "User");

			IHttpActionResult addRoleResult = GetErrorResult(roleResult);

			if (addRoleResult != null)
			{
				return addRoleResult;
			}

			string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
			var callbackUrl = Url.Link("ConfirmEmail", new { userId = user.Id, code = code });

			var notification = new AccountNotificationModel
			{
				Code = code,
				Url = callbackUrl,
				UserId = justCreatedUser.Id,
				Email = justCreatedUser.Email,
				DisplayName = justCreatedUser.UserName
			};

			string body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml", notification);
			await UserManager.SendEmailAsync(user.Id, "DurandalAuth account confirmation", body);

			return Ok();
		}
예제 #2
0
		public async Task<IHttpActionResult> ResendConfirmationEmail()
		{
			UserProfile user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
			string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
			var callbackUrl = Url.Link("ConfirmEmail", new { userId = user.Id, code = code });

			var notification = new AccountNotificationModel
			{
				Code = code,
				Url = callbackUrl,
				UserId = user.Id,
				Email = user.Email,
				DisplayName = user.UserName
			};

			string body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml", notification);
			await UserManager.SendEmailAsync(user.Id, "DurandalAuth account confirmation", body);
			
			return Ok();
		}
예제 #3
0
		public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordBindingModel model)
		{
			if (ModelState.IsValid)
			{
				var user = await UserManager.FindByEmailAsync(model.Email);
				if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
				{
					ModelState.AddModelError("", "The user either does not exist or is not confirmed.");
					return BadRequest(ModelState);
				}

				string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
				var callbackUrl = Url.Content("~/account/resetpassword?email=") + HttpUtility.UrlEncode(model.Email) + "&code=" + HttpUtility.UrlEncode(code);

				var notification = new AccountNotificationModel
				{
					Url = callbackUrl,
					DisplayName = user.UserName
				};

				string body = ViewRenderer.RenderView("~/Views/Mailer/PasswordReset.cshtml", notification);
				await UserManager.SendEmailAsync(user.Id, "DurandalAuth reset password", body);
				
				return Ok();
			}

			// If we got this far, something failed
			return BadRequest(ModelState);
		}
예제 #4
0
        public async Task<IHttpActionResult> Register(RegisterBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var user = new UserProfile { UserName = model.UserName, Email = model.Email, EmailConfirmed = false };
           
            var identityResult = await this.UserManager.CreateAsync(user, model.Password);

            var createResult = this.GetErrorResult(identityResult);

            if (createResult != null)
            {
                return createResult;
            }

            var justCreatedUser = await this.UserManager.FindByNameAsync(model.UserName);

            /*var profile = new Profile
                              {
                                  UserId = justCreatedUser.Id
                              };*/

           var roleResult = await this.UserManager.AddToRoleAsync(justCreatedUser.Id, "User");

            var addRoleResult = this.GetErrorResult(roleResult);

            if (addRoleResult != null)
            {
                return addRoleResult;
            }

            var code = await this.UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
            var callbackUrl = this.Url.Link("ConfirmEmail", new { userId = user.Id, code });

            var notification = new AccountNotificationModel
                                   {
                                       Code = code,
                                       Url = callbackUrl,
                                       UserId = justCreatedUser.Id,
                                       Email = justCreatedUser.Email,
                                       DisplayName = justCreatedUser.UserName
                                   };

            var body = ViewRenderer.RenderView("~/Views/Mailer/NewAccount.cshtml", notification);
            await this.UserManager.SendEmailAsync(user.Id, "Home Service account confirmation", body);

            //return this.Created(this.Request.RequestUri + justCreatedUser.Id, profile);

            return this.Ok(justCreatedUser.Id);
        }