Email activation problem

Last post 02-28-2008 6:58 PM by worldcrafter. 6 replies.

Sort Posts:

  • Email activation problem

    01-19-2007, 9:44 AM

    Hello guys,

    How's ya day goin cos mines been mean. Basically, im using asp.net 2 with c# using a ms sql 2005 database in visual studio 2005. My database has been modified with aspnet_regsql.exe  and login and createuser works fine. Basically i want to activate a user via email. Now i've figured out how to send the email but i don't know how to associate it with the UserID. So that when the user clicks on the link found within the email, they will be taken to activate.aspx where their account will be activated. My email looks as follows:

     MembershipUser user = Membership.GetUser(cuw.UserId);  
            MailMessage message = new
            MailMessage("myemail@myemail.co.uk",CreateUserWizard1.Email);
            message.Subject =" Activation";
            message.Body = "Thank You for registering with the site. We need to make sure you are who you say you are. If you are the individual who registered with us, please click on the following link:http://www.xxxxxxxxxxxxxx.com/activate.aspx .";
            SmtpClient client = new SmtpClient("mail.me.com");
            client.Send(message);

    How do i associate the UserId to this email and the link? And how do i do 'User.IsApproved' on the activation.aspx page?

    I think im half way there in figuring out how to do this. Please help me
    x
    Laura

  • Re: Email activation problem

    01-19-2007, 11:21 AM
    Answer
    • Loading...
    • Yani Dzhurov
    • Joined on 11-23-2006, 2:02 PM
    • Sofia, Bulgaria
    • Posts 516

    Hi,

    you could pass the User Id to the link in your mail.

    For example:

     message.Body = "Thank You for registering with the site. We need to make sure you are who you say you are. If you are the individual who registered with us, please click on the following link:http://www.xxxxxxxxxxxxxx.com/activate.aspx?UserId=50 ."

    Then, in the activate.aspx page you check the query string parameters by Request["UserId"] and if it is not null, this mean there is such parameter passed.

    So you could do the activation of the account of user with id '50'.

    Cheers,

    Yani
     

  • Re: Email activation problem

    01-21-2007, 9:22 PM
    Answer

    Every MembershipUser has a property named "ProviderUserKey" (Basically a GUID) which is unique. When Generating the activation link in your mail append it as a QueryString like the following:

    message.Body = "Thank You for registering with the site. We need to make sure you are who you say you are. If you are the individual who registered with us, please click on the following link:http://www.xxxxxxxxxxxxxx.com/activate.aspx?userId=" + user.ProviderUserKey.ToString() + ".";

    And When the User Clicks on the Activation Link, in your Activation.aspx:

    string userId = Request.QueryString["userId"];

    MembershipUser user = Membership.GetUser(userId);
    user.IsApproved = true;
    Membership.UpdateUser(user);

    Thats All.

    Pls mark it as answer if it solves your problem.



     

    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    http://weblogs.asp.net/rashid/
  • Re: Email activation problem

    01-25-2007, 11:56 PM
    • Loading...
    • kiwi_cyh
    • Joined on 08-04-2006, 9:39 PM
    • Posts 54

    hi KaziManzurRashid,

    it seems not working, i got this error on the user.IsApproved = true ; //{"Object reference not set to an instance of an object."}

    what is the problem? Thanks.
     

    kiwi_cyh
  • Re: Email activation problem

    01-29-2007, 4:42 PM
    • Loading...
    • Peaks
    • Joined on 01-29-2007, 9:27 PM
    • Posts 1

     

    Hi laura,

    I had a similar problem and discovered this solution: - 

    In the CreateUserWizard1_CreatedUser event

    CreateUserWizard cuw = ((CreateUserWizard)(sender));
    MembershipUser user = Membership.GetUser(cuw.UserName);
    Guid userId = ((Guid)(user.ProviderUserKey));
    const string FromAddress = "myemail@myemail.co.uk";
    MailMessage mm = new MailMessage(FromAddress, cuw.Email);
    mm.Subject = "Activation";
    mm.Body = "Thank you for registering with our site.Please click on the following link: http://www.xxxxxxxxxxxxxx.com/activate.aspx?userId=" + userId.ToString() + " to activate your username.";
    SmtpClient smtp = new SmtpClient();
    smtp.Send(mm);

     In the Page load event of the activation.aspx

    string userId = Request.QueryString("userId");
    Guid oGuid = new Guid(userId);
    MembershipUser oUser = Membership.GetUser(oGuid);
    if (!((oUser == null)) && oUser.IsApproved == false) {
     oUser.IsApproved = true;
     Membership.UpdateUser(oUser);
      System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName, false);
    }

     If you want to assign the user to a role use the below:-

    Roles.AddUserToRole(oUser.ToString, "AuthorisedUsers");

    Hope this helps.

    Peaks

     


  • Re: Email activation problem

    02-19-2008, 7:01 AM

     Thanks for help but i want to know will i add anything in web.config file

    and also is  http://www.xxxxxxxxxxxxxx.com/activate.aspx?userId=" + userId.ToString() + " to activate your username.";

    the link that will be send to user ???

    http://www.yourDomain.com/activate.aspx?userId=" + userId.ToString();

    is that correct ???

    Thanks again for help 

     

     

    Regards,
    Yasser Zaid

    ~ Please remember to click Mark as Answer on this post if it helped you ~
  • Re: Email activation problem

    02-28-2008, 6:58 PM
    • Loading...
    • worldcrafter
    • Joined on 02-28-2008, 6:09 PM
    • Hawaii
    • Posts 15

    yay my first post. yes Laura, that is the link that will be sent to the user. userId.ToString() will be replaced by the ProviderUserKey value that is stored in your membership provider database. 

    I tried your code out Peaks, works great, excep I had to change

    string userId = Request.QueryString("userId");

    to

    string userId = Request.QueryString.Get("userId");

    I also added response.writes to let the user know the status of their activation.

    protected void Page_Load(object sender, EventArgs e)
        {
            string userId = Request.QueryString.Get("userId");
            Guid oGuid = new Guid(userId.ToString());
           
            //get the user so that we can approve the account.
            MembershipUser oUser = Membership.GetUser(oGuid);

            if (!((oUser == null)) && oUser.IsApproved == false)
            {
                oUser.IsApproved = true;
                Membership.UpdateUser(oUser);

                //below code navigates to a url requested beforehand
                //System.Web.Security.FormsAuthentication.RedirectFromLoginPage(oUser.UserName, false);    

                //write a success message to the screen
                Response.Write("You have successfully activated your account.");
            }
            else Response.Write("Your account has already been activated.");
        }

    It'd probably be a good idea to say that the IsLockedOut property is what you should use to lock accounts, not the IsApproved property, since users can always use the link that you generate to reapprove their accounts if it is manually unapproved by you.

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter