Search code examples
c#asp.net-mvcentity-frameworksecuritysimplemembership

SimpleMembership - WebSecurity GetConfirmationToken Problems


I'm building an application which is close to completion, I'm adding in the final layers of security and functionality for emailing new user's a confirmation token when they register. Getting the initial registration confirmation token was easy enough. The token is passed to the view and an email is sent out. However, there are two scenarios where I need to resend that token, these being:

  • When the user is redirected to a view called 'Registration Step Two'.
  • When a user tries logging in before they have confirmed their account.

The first scenario has a button on the page which could easily have the token embedded, making this a simple task. However, the login page will not have this functionality, the server will only know the user's email address. Keeping to the DRY principle I am merging this into one method, however whilst I can get the email address the user registered with I can't find an appropriate WebSecurity or Membership call which gets the confirmation token.

I've tried requesting this from the membership table using the below Expression, however an exception is thrown stating the LastPasswordFailureDate is null (which is to be expected), so this wasn't possible.

Expression<Func<MembershipModel, bool>> query = q => q.UserId == userId;

I've looked at blogs similar to Kevin Junghans (below), however WebSecurity.GetConfirmationToken doesn't seem to be supported anymore as I cannot locate it anywhere.

Retrieving Confirmation Token

I've even seen the next link, which points at a question on StackOverflow and to be honest I don't want to be implementing it in this manor as I've spent a vast amount of time incorporating an EntityFramework repository pattern to do all this for me.

Resend Confirmation Email with WebSecurity

I'm wondering if anyone has any recommendations on getting the confirmation token without writing the SQL within the application itself.


Solution

  • Whilst thinking on it a little more this morning, I finally realised why the LastPasswordFailureDate was returning the mentioned Exception. Essentially I believe I am an idiot and have forgotten to add a nullable reference to the DateTime fields within my Model. I can't test this for fix for several hours, but will update this answer/question in either event.