public async Task<IHttpActionResult> PostInvite(WebApiSucksSoMuch inviteEmail) { if (inviteEmail.Email == "") { string error = "noEmail"; return Ok(error); } var user = db.Users.Find(User.Identity.GetUserId()); InviteCode code = new InviteCode(); var invite = db.Invitation.FirstOrDefault(i => i.InvitedEmail == inviteEmail.Email && i.HouseholdId == user.HouseholdId); if(invite == null) { invite = new Invitaton() { Code = code.CreateCode(), HouseholdId = user.HouseholdId, InvitedEmail = inviteEmail.Email }; db.Invitation.Add(invite); } var invitedUserExists = db.Users.Any(u => u.Email == inviteEmail.Email); //var url = invitedUserExists ? Url.Route("Go to join view", "") : Url.Route("Go to register and join view", ""); var mailer = new EmailService(); var message = new IdentityMessage() { Subject = "You've been invited", Destination = inviteEmail.Email, Body = "You've been invited to join " + user.DisplayName + "'s household. This is your join code: " + invite.Code }; await db.SaveChangesAsync(); await mailer.SendAsync(message); return Ok(invite); }
public static void SendEmail(this ApplicationUser user, string sub, string txt) { EmailService es = new EmailService(); es.SendAsync(new IdentityMessage { Destination=user.Email, Subject=sub, Body=txt}); }
public async Task<IHttpActionResult> PostInvite(string inviteEmail) { var user = db.Users.Find(User.Identity.GetUserId()); var code = new InviteCode(); var inviteExists = db.Invitations.Any(i => i.InvitedEmail == inviteEmail); var invite = new Invitation(); if (inviteExists) { invite = db.Invitations.Where(i=> i.InvitedEmail == inviteEmail).FirstOrDefault(); } else { invite = new Invitation() { Code = code.MakeCode(), HouseHoldId = user.HouseHoldId, InvitedEmail = inviteEmail }; db.Invitations.Add(invite); } var invitedUserExists = db.Users.Any(u=> u.Email == inviteEmail); //var url = invitedUserExists ? Url.Link("JoinHouseHoldEU", "") : Url.Link("JoinHouseHoldNU", ""); var mailer = new EmailService(); var message = new IdentityMessage() { Subject = "New Invitation from rgoodwin-budget.azurewebsites.net", Destination = inviteEmail, Body = "You have been invited to join " + user.UserName + "'s HouseHold. To join please follow this <a href=\'" + "\'>link </a> and use this code: " + invite.Code }; await db.SaveChangesAsync(); await mailer.SendAsync(message); return Ok(invite); }
private async Task<IHttpActionResult> SendLoginInformation(RegisterBindingModel model) { EmailService emailService = new EmailService(); IdentityMessage message = new IdentityMessage() { Subject = "Login Information", Body = String.Format("Your username {0} and password: {1}", model.Email, model.Password), Destination = model.Email }; try { await emailService.SendAsync(message); } catch (Exception e) { return InternalServerError(); //TODO: display proper error } return Ok(); }
private async Task<IHttpActionResult> SendEmailConfirmation(string userId, string emailDestination) { var code = await UserManager.GenerateEmailConfirmationTokenAsync(userId); code = HttpUtility.UrlEncode(code); //Bug fixed, url not encoded during creation of token. EmailService emailService = new EmailService(); string callbackUrl = GetBaseUrl() + "#/confirmEmail/" + userId + "/" + code; IdentityMessage message = new IdentityMessage() { Subject = "Confirm your account", Body = "Please confirm your account by clicking this link: " + callbackUrl, Destination = emailDestination }; try { await emailService.SendAsync(message); } catch (InvalidOperationException ioe) { return InternalServerError(); //TODO: display proper error } return Ok(); //await UserManager.SendEmailAsync() //Why not using this? }
public async Task<IHttpActionResult> EmailForgotPasswordToken(string email) { ApplicationUser user = await UserManager.FindByEmailAsync(email); if (user == null) { return BadRequest(); } var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id); code = HttpUtility.UrlEncode(code); EmailService emailService = new EmailService(); string callbackUrl = GetBaseUrl() + "#/resetPassword/" + user.Id + "/" + code; IdentityMessage message = new IdentityMessage() { Subject = "Reset password", Body = "Please reset your account by clicking this link: " + callbackUrl, Destination = email }; try { await emailService.SendAsync(message); } catch (InvalidOperationException ioe) { return InternalServerError(); //TODO: display proper error } return Ok(); }
public async Task<IHttpActionResult> SendInvite(string email) { var user = db.Users.Find(User.Identity.GetUserId()); if (user.HouseholdId != null) { var inviteExists = db.Invitations.Any(i => i.InvitedEmail == email); var invite = new Invitation(); if (inviteExists) invite = db.Invitations.Where(i => i.InvitedEmail == email).FirstOrDefault(); else { invite = new Invitation() { Code = GetRandomString(8), HouseholdId = (int) user.HouseholdId, InvitedEmail = email }; db.Invitations.Add(invite); await db.SaveChangesAsync(); } var invitedUserExists = db.Users.Any(u => u.Email == email); var mailer = new EmailService(); var url = Url.Link("Join", new { inviteEmail = email, inviteCode = invite.Code}); var message = new IdentityMessage() { Subject = "You've Been Invited To A Household!", Body = "You have been invited to join " + user.Email + "'s Household. To Join please follow " + "<a href=\'" + url + "\'>this link</a>.", Destination = email }; await mailer.SendAsync(message); return Ok(invite); } return Ok("User is not in a household."); }
public AccountService Create() { var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext())); var emailService = new EmailService(); return new AccountService(userManager, roleManager, emailService); }