开发者

How to send link/url to confirm user registration and/or password reset/recovery using ASP.Net MVC2?

I see it all over the place, yet, I could not find one example about this (maybe I don't know the proper wording), I am trying to build using ASP .Net MVC2 (but any example on just ASP .Net would be also helpful) a process that will send a link to the user at the end of the registratio开发者_开发技巧n process to let him confirm his registration. Also, a similar process to let the user to reset his password, with the typical "forgot password" and send a link/url so that the user can click and type a new password. Can someone help me to either find an example or at least to let me know how to "google" it?

Thanks, Mark


For the forgot password, you can make a view like this

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true)%>

<p><%: ViewData["Error"] %></p>
<p> Have you forgotten you password? No problem!</p>

<div class="editor-label">
    <%: Html.Label("Fill in your username") %>
</div>
<div class="editor-field">
    <%: Html.TextBox("userName") %>
</div>

 <p> and <input type="submit" value="click here" /> to reset your password.</p> 

<% } %>

and as a controller (first make an model from the aspnetdb (if you don't see it press the 'show all files' button))

This must be placed right after the controller definition

aspnetdbEntities aspnetdb = new aspnetdbEntities();

Then follows this

public ActionResult ForgottenPassword()
    {
        return View();
    }

[HttpPost]
public ActionResult ForgottenPassword(FormCollection formValue) 
{ 
   var userName = formValue["userName"];
   try
        {
            var useraccount = aspnetdb.aspnet_Users.Single(c => c.UserName == userName);
            var fromAddress = "put an email-address";

            var message = new MailMessage(fromAddress, user.Email)
            {
                Subject = "",
                Body = "a link to a controller that lets the user put in a 
                        new password and then save that password to the aspnetdb."
                        (that controller will most likley require a username)
                        "or a link with a new random password in it tht you have put
                        as his password like this:"
                        useraccount.aspnet_Membership.Password = "random password";
                        aspnetdb.SaveChanges;

             };

            var client = new SmtpClient("smtpServerName");
            client.Send(message);

            return View();
        }

        catch
        {
            ViewData["Error"] = "Please give up an existing username";
            return View();


        }
    }

I hope this answer was helpfull.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜