shaun
shaun

Reputation: 1273

What is the best way to Confirm Email in Web API 2 using Identity 2?

I am wanting to build an ASP.net Web API using Identity 2 using the current templates in Visual Studio.

I want to confirm an email address an account is created with is a valid email address.

I can't really find much of a how to guide using the current templates.

I found http://bitoftech.net/2015/02/03/asp-net-identity-2-accounts-confirmation-password-user-policy-configuration/ that builds if from scratch however things are very confusing. I am looking at his code for the register action of the AccountController.

string code = await this.AppUserManager.GenerateEmailConfirmationTokenAsync(user.Id);

var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));

await this.AppUserManager.SendEmailAsync(user.Id,"Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

return Created(locationHeader, TheModelFactory.Create(user));

my current code is:

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
        // Send an email with this link
        string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
        code = HttpUtility.UrlEncode(code);
        var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code }));
        await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
        Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));

        return Created(locationHeader, );

His TheModelFacory.Create I can't find anywhere. I was able to find TheModelFactory mentioned in a BaseApiController : ApiController he created for his project but can't for the life of me figure out where this would fit into the current Web Api with Individual User Accounts Templates.

What do I put as T Content instead of TheModelFactory.Create(user) to get this to work?

Upvotes: 2

Views: 3529

Answers (2)

Nkosi
Nkosi

Reputation: 247068

There is a link in the article that takes you to the source code on GitHub

Here is the link to the ModelFactory.cs which is used to create models and copy the information you want to return over to it.

public class ModelFactory
{

    private UrlHelper _UrlHelper;
    private ApplicationUserManager _AppUserManager;

    public ModelFactory(HttpRequestMessage request, ApplicationUserManager appUserManager)
    {
        _UrlHelper = new UrlHelper(request);
        _AppUserManager = appUserManager;
    }

    public UserReturnModel Create(ApplicationUser appUser)
    {
        return new UserReturnModel
        {
            Url = _UrlHelper.Link("GetUserById", new { id = appUser.Id }),
            Id = appUser.Id,
            UserName = appUser.UserName,
            FullName = string.Format("{0} {1}", appUser.FirstName, appUser.LastName),
            Email = appUser.Email,
            EmailConfirmed = appUser.EmailConfirmed,
            Level = appUser.Level,
            JoinDate = appUser.JoinDate,
            Roles = _AppUserManager.GetRolesAsync(appUser.Id).Result,
            Claims = _AppUserManager.GetClaimsAsync(appUser.Id).Result
        };

    }

    public RoleReturnModel Create(IdentityRole appRole) {

        return new RoleReturnModel
       {
           Url = _UrlHelper.Link("GetRoleById", new { id = appRole.Id }),
           Id = appRole.Id,
           Name = appRole.Name
       };

    }
}

And here is a snippet of the BaseApiController.cs that your AccountController inherits from.

public class BaseApiController : ApiController
{

    private ModelFactory _modelFactory;
    private ApplicationUserManager _AppUserManager = null;
    private ApplicationRoleManager _AppRoleManager = null;

    protected ApplicationUserManager AppUserManager
    {
        get
        {
            return _AppUserManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
    }

    public BaseApiController()
    {
    }

    //...code removed for brevity

    protected ModelFactory TheModelFactory
    {
        get
        {
            if (_modelFactory == null)
            {
                _modelFactory = new ModelFactory(this.Request, this.AppUserManager);
            }
            return _modelFactory;
        }
    }
}

Upvotes: 2

Adetayo
Adetayo

Reputation: 31

You don't necessarily have to return a 201 Created response in your Register Method as he did. For simplicity, you can just return a OK() which is a 200 Response.

However, if you must follow his example using the exact Created response, you can simply paste this code below in the Helpers region (or just any where) of your AccountController.cs file

protected ModelFactory TheModelFactory
{
    get
    {
       if (_modelFactory == null)
       {
          _modelFactory = new ModelFactory(this.Request, UserManager);
       }
            return _modelFactory;
    }
}

You should be fine after this.

Upvotes: 0

Related Questions