Skip to content

Instantly share code, notes, and snippets.

@DavidVeksler
Created September 6, 2015 11:22
Show Gist options
  • Select an option

  • Save DavidVeksler/2fd2775a3774125faf81 to your computer and use it in GitHub Desktop.

Select an option

Save DavidVeksler/2fd2775a3774125faf81 to your computer and use it in GitHub Desktop.

Revisions

  1. DavidVeksler created this gist Sep 6, 2015.
    54 changes: 54 additions & 0 deletions ForgotPassword.cshtml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    @using FEE.Domain
    @inherits UmbracoTemplatePage
    @{
    Layout = "FEEMaster.cshtml";
    var featuredImage = CoverImageProvider.GetCoverImageOrDefault(CurrentPage.featuredImage);
    }


    @section bodyClass {subpage}
    @*TODO maybe implement:https://github.com/warrenbuckley/CWS-Umbraco-Standard-Membership/blob/master/CWSUmbracoStandardMembership/Views/AuthSurface/ResetPassword.cshtml
    This html taken from https://our.umbraco.org/member/forgot-password/ which uses webforms
    *@

    <script>
    function sendreset() {
    // todo: replace with AJAX and junk
    window.location = '/Umbraco/Api/UserAPI/ResetPasswordForUser?email=' + document.getElementById("email").value;
    }
    </script>

    <div class="main-display" style="height: 120px;">
    <img src="@featuredImage.Url" alt="@featuredImage.Name" width="1380px" height="120px"/>

    <div class="container">
    <h1 style="top: 25px;">@Umbraco.Field("pageName")</h1>
    </div>
    </div>

    <section>
    <div class="container">
    <div class="content">
    <div class="content-holder">
    <div class="txt-holder" style="width: 100%">
    <p class="abstract">
    Please enter your email address, and we will generate a new password for you and send it straight to your inbox.
    </p>

    <div id="umbracoAutoForm">
    <p>
    <label>Enter your email:</label>
    <input name="tb_email" type="text" id="email" class="field"/>
    </p>
    <br/>
    <div id="umbracoFormNavigation">
    <input type="submit" name="" value="Retrieve my password" id="bt_retrieve" onclick="sendreset();"/>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>
    </section>
    136 changes: 136 additions & 0 deletions UserAPIController.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    using System;
    using System.Configuration;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Http;
    using System.Web.Security;
    using FEE.Domain.Networking;
    using FluentEmail;
    using umbraco;
    using Umbraco.Core.Logging;
    using Umbraco.Web.WebApi;

    namespace FEE.Web.Controllers
    {
    public class UserAPIController : UmbracoApiController
    {
    private readonly string _adminEmail = ConfigurationManager.AppSettings["DefaultFromEmailAddress"];

    // CMS-117
    // http://local.fee.org/Umbraco/Api/UserAPI/SendUserEmail?message=mypassword&[email protected]
    // Inspired by https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/63159-Reset-user-password-and-email-new-password
    [AcceptVerbs("GET", "POST")]
    public bool SendUserEmail([FromBody] PasswordResetCredentialsModel message)
    {
    try
    {
    var emailMessage = @"<h3>Password reset for @Model.Name from @Model.Website</h3>
    <p>Your password for the @Model.Website website has been reset to: @Model.Password</p>" +
    "<p>If you have received this email in error please notify the systems manager at <a href='mailto:@Model.AdminEmail'>@Model.AdminEmail</a>. The email should then be deleted.</p>";

    var email = Email
    .From(_adminEmail)
    .To(message.emailAddress, message.name)
    .Subject("Password reset from " + Request.RequestUri.Host)
    .UsingTemplate(emailMessage,
    new
    {
    Website = Request.RequestUri.Host,
    AdminEmail = _adminEmail,
    Password = message.message,
    Name = message.name
    }, true);

    email.Send();


    return true;
    }
    catch (Exception ex)
    {
    LogHelper.Error<UserAPIController>("failed to send password notification", ex);
    throw;
    }
    }

    // http://local.fee.org/Umbraco/Api/UserAPI/[email protected]
    [AcceptVerbs("GET", "POST")]
    public string ResetPasswordForUser(string email)
    {
    // Try and get the member from the email address entered
    var cMember = ApplicationContext.Services.UserService.GetByEmail(email);

    // Check we have a user with that email address
    if (cMember == null)
    {
    return ResetPasswordForMember(email);
    }

    // Found the user
    // Generate a password which we'll email the member
    var password = Membership.GeneratePassword(10, 1);
    password = Regex.Replace(password, @"[^a-zA-Z0-9]", m => "9");

    // Change the password to the new generated one above
    ApplicationContext.Services.UserService.SavePassword(cMember, password);


    // Now email the member their password
    var sb = new StringBuilder();
    sb.AppendFormat("<p>Please find your new password below to access the site</p>");
    sb.AppendFormat("<p><b>{0}</b></p>", password);
    sb.AppendFormat(
    "<p>(This request was made from {0}. If you did not make this request, please contact an administrator right away.)</p>",
    HttpContext.Current.Request.GetOriginalHostAddress());
    library.SendMail(_adminEmail, cMember.Email, "Password Reset", sb.ToString(), true);


    //// Show a message to the user
    return "Password Sent. Please check your email.";
    }

    // http://fee-dev.org/Umbraco/Api/UserAPI/[email protected]
    [AcceptVerbs("GET", "POST")]
    public string ResetPasswordForMember(string email)
    {
    // Try and get the member from the email address entered
    var cMember = ApplicationContext.Services.MemberService.GetByEmail(email);

    // Check we have a member with that email address
    if (cMember != null)
    {
    // Found the user
    // Generate a password which we'll email the member
    var password = Membership.GeneratePassword(10, 1);
    password = Regex.Replace(password, @"[^a-zA-Z0-9]", m => "9");

    // Change the password to the new generated one above
    ApplicationContext.Services.MemberService.SavePassword(cMember, password);
    // Save the password/member


    // Now email the member their password
    var sb = new StringBuilder();
    sb.AppendFormat("<p>Please find your new password below to access the site</p>");
    sb.AppendFormat("<p><b>{0}</b></p>", password);
    sb.AppendFormat(
    "<p>(This request was made from {0}. If you did not make this request, please contact an administrator right away.)</p>",
    HttpContext.Current.Request.GetOriginalHostAddress());
    library.SendMail(_adminEmail, cMember.Email, "Password Reset", sb.ToString(), true);

    //// Show a message to the user
    return "Password Sent. Please check your email.";
    }
    // Can't find a user with that email
    return "Can't find a user with that email address";
    }
    }

    public class PasswordResetCredentialsModel
    {
    public string message { get; set; }
    public string emailAddress { get; set; }
    public string name { get; set; }
    }
    }
    3 changes: 3 additions & 0 deletions login.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Add this inside <div class="form">

    <div class=""><a style="color: #fff;" href="/my-account/forgot-password/">Reset your password</a></div>
    49 changes: 49 additions & 0 deletions passwordChanger.ascx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    <%@ Control Language="C#" AutoEventWireup="True" CodeBehind="passwordChanger.ascx.cs" Inherits="Umbraco.Web.UI.Umbraco.Controls.PasswordChanger" %>

    (changes begin here:)


    <div id="Div1" runat="server" class="alert alert-success" style="margin-top: 10px; width: 300px;" visible="<%# string.IsNullOrWhiteSpace(ChangingPasswordModel.GeneratedPassword) == false %>">
    <p style="text-align: center">
    Password has been reset and send to the user by email.<br />
    <br />
    <%--<strong><%# ChangingPasswordModel.GeneratedPassword %></strong>--%>
    </p>

    <script type="text/javascript">
    $(document).ready(function() {
    //debugger;
    var newPassword = "<%# ChangingPasswordModel.GeneratedPassword %>";
    var emailAddress = document.getElementById("body_ctl19").value;
    var name = document.getElementById("body_ctl12").value;
    $.ajax({
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    url: '/Umbraco/Api/UserAPI/SendUserEmail',
    data: '{ "emailAddress": "' + emailAddress + '", "message": "' + newPassword + '", "name": "' + name + '"}',
    success: function(data) {
    //alert("<h3>Update successful</h3> <p>An email with the new password has been sent to: " + emailAddress + ".</p>");
    //console.log(data);
    $('#passwordInfobox').show();
    $('#passwordInfobox').html("<h3>Update successful</h3> <p>An email with the new password has been sent to: " + emailAddress + ".</p>");
    },
    error: function(jqXHR, textStatus, errorThrown) {
    console.log(textStatus, errorThrown);
    ('#passwordInfobox').show();
    ('#passwordInfobox').html("<h3>There was a problem with your request</h3> <p>There was a problem updating the password, please try again.</p)<p>" + errorThrown + "</p>");
    }
    });
    });
    </script>

    </div>