Advertisement
Guest User

Untitled

a guest
Feb 20th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. using System.Web.Http.Cors;
  8. using System.Web.Security;
  9. using WorkAtHeightWebApp.Models;
  10.  
  11. namespace WorkAtHeightWebApp.Controllers
  12. {
  13.     [EnableCors("*", "*", "*")]
  14.     public class AccountController : ApiController
  15.     {
  16.         [AcceptVerbs("GET", "POST")]
  17.         public object GetAuthToken(string email, string password)
  18.         {
  19.             using (var db = new WorkAtHeightContext())
  20.             {
  21.                 var userDetails = db.CompanyUsers.Where(m => m.EmailAddress.Equals(email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
  22.                 if (userDetails == null || !PasswordHash.ValidatePassword(password, userDetails.Password))
  23.                 {
  24.                     throw new Exception("The credentials you have provided are invalid.");
  25.                 }
  26.                 else
  27.                 {
  28.                     FormsAuthenticationTicket ticket =
  29.                         new FormsAuthenticationTicket(1, userDetails.EmailAddress,
  30.                             DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout),
  31.                             false, userDetails.UserType);
  32.  
  33.                     string ticketString = FormsAuthentication.Encrypt(ticket);
  34.  
  35.                     return new { AccessToken = ticketString, UserRole = userDetails.UserType };
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement