title: Token Based Authentication using ASP.NET Web API 2, Owin, AspNet.Identity.MongoDB and mongocsharpdriver
body: <p>+after successfully implementing <strong>Token Based Authentication using ASP.NET Web API 2, Owin, and Identity</strong>, i wished to change my implementation to use MongoDB instead of MSSQL with Entity Framework, with the help of this <a href="https://github.com/attilah/AngularJSAuthentication" rel="nofollow"><strong>application here</strong></a>....truth be said, i dont fully understand how this should be done, but at least i know what i want my application to behave. i want to follow this <a href="http://www.joe-stevens.com/2011/10/02/a-mongodb-tutorial-using-c-and-asp-net-mvc/" rel="nofollow"><strong><em>IMPLEMENTATION HERE</em></strong></a>, using <em>AspNet.Identity.MongoDB</em> <strong>and</strong> <em>mongocsharpdriver</em>...and so far, here,s what I've done:
  <strong>Account Controller</strong></p>
  
  <pre><code>using Microsoft.AspNet.Identity;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Threading.Tasks;
  using System.Web.Http;
  using withMongoDB.HelperClasses.Services;
  using withMongoDB.Models.Account;
  
  namespace withMongoDB.Controllers
  {
      [RoutePrefix("api/Account")]
      public class AccountsController : ApiController
      {
          AccountsService _accountsService = new AccountsService();
  
          // POST api/Account/Register
          [AllowAnonymous]
          [Route("Register")]
          public async Task&lt;IHttpActionResult&gt; Register(UserModel userModel)
          {
              if (!ModelState.IsValid)
              {
                  return BadRequest(ModelState);
              }
  
              await _accountsService.Register(userModel);
  
              return Ok();
          }
  
          private IHttpActionResult GetErrorResult(IdentityResult result)
          {
              if (result == null)
              {
                  return InternalServerError();
              }
  
              if (!result.Succeeded)
              {
                  if (result.Errors != null)
                  {
                      foreach (string error in result.Errors)
                      {
                          ModelState.AddModelError("", error);
                      }
                  }
  
                  if (ModelState.IsValid)
                  {
                      // No ModelState errors are available to send, so just return an empty BadRequest.
                      return BadRequest();
                  }
  
                  return BadRequest(ModelState);
              }
  
              return null;
          }
      }
  }
  </code></pre>
  
  <p>then the register method from the controller should be taken by the <strong>accounts Service</strong></p>
  
  <pre><code>using AspNet.Identity.MongoDB;
  using Microsoft.AspNet.Identity;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using System.Web;
  using withMongoDB.Models.Account;
  
  namespace withMongoDB.HelperClasses.Services
  {
      public class AccountsService 
      {
          private readonly MongoAccountsConnectionHelper&lt;UserProfile&gt; _accounts;
  
          public AccountsService()
          {
              _accounts = new MongoAccountsConnectionHelper&lt;UserProfile&gt;();
          }
  
          public async Task&lt;IdentityResult&gt; Register(UserModel userModel)
          {
              var userprofile = new UserProfile
              {
                  UserName = userModel.UserName
              };
  
              var result = await _accounts.CreateAsync(userprofile, userModel.Password);
  
              return result;
          }
      }
  }
  </code></pre>
  
  <p>and finally the <strong>MongoAccountsConnectionHelper</strong> takes the result of the accounts service class to mongo database....</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace withMongoDB.HelperClasses 
  {
      using AspNet.Identity.MongoDB;
      //using Entities;
      using MongoDB.Bson;
      using MongoDB.Bson.Serialization.Conventions;
      using MongoDB.Driver;
      using System.Configuration;
      using withMongoDB.Models.Account;
      public class MongoAccountsConnectionHelper
      {
          private readonly MongoCollection&lt;UserProfile&gt; userProfileCollection;
          public MongoDatabase Database { get; private set; }
          public MongoAccountsConnectionHelper()
          {
              var pack = new ConventionPack()
              {
                  new CamelCaseElementNameConvention(),
                  new EnumRepresentationConvention(BsonType.String)
              };
  
              ConventionRegistry.Register("CamelCaseConvensions", pack, t =&gt; true);
  
              var mongoUrlBuilder = new MongoConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString);
              Database = new MongoClient().GetServer().GetDatabase(mongoUrlBuilder.DatabaseName);
  
              userProfileCollection = Database.GetCollection&lt;UserProfile&gt;("users");
          }
  
          public MongoCollection&lt;UserProfile&gt; Users
          {
              get { return userProfileCollection; }
          }
      }
  }
  </code></pre>
  
  <p>any help, tips, ideas, or opinions will be highly appreciated....{<strong>should i consider alternatives like MembershipReboot and IdentityReboot by brockallen?</strong>}</p>