Search code examples
asp.net-mvc-4simplemembership

Get userId through ConfirmationToken


My application is performing email validation sending an email to the user. When the user click on the link, the following action is called:

[AllowAnonymous]
    public ActionResult RegisterConfirmation(string Id) //Id=ConfirmationToken
    {
        if (WebSecurity.ConfirmAccount(Id))
        {
            //get userId and perfom some operations related to it
            return RedirectToAction("ConfirmationSuccess");
        }
        return RedirectToAction("ConfirmationFailure");
    }

My question is:

How can I get the userId using the ConfirmationToken?

I'm using SimpleMembershipProvider and I have acces to my database context.


Solution

  • That information is in the webpages_Membership table and you can use the context for SimpleMembership to directly query for that information by using context.Database.SqlQuery.

    select UserId from webpages_Membership where ConfirmationToken = {0}
    

    That being said, I would not recommend using this to automatically log the person in after confirmation is complete, if that is your intention. There are some security issues that can be caused by doing this as discussed in the comments of this article.