Skip to content

Instantly share code, notes, and snippets.

@swlaschin
Created December 30, 2014 21:28
Show Gist options
  • Save swlaschin/264891753f8ac70b55d0 to your computer and use it in GitHub Desktop.
Save swlaschin/264891753f8ac70b55d0 to your computer and use it in GitHub Desktop.
namespace AwesomeAngularMVCApp.Controllers
open System.Threading.Tasks
open System.Web
open System.Web.Mvc
open AwesomeAngularMVCApp.Models
open Microsoft.AspNet.Identity.Owin
open System.Web.Http.Owin
[<Authorize>]
type AccountController(userManager:ApplicationUserManager, signInManager:ApplicationSignInManager) =
inherit Controller()
// secondary constructor with implicit params
new() =
let userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()
let signInManager = HttpContext.GetOwinContext().Get<ApplicationSignInManager>()
new AccountController(userManager,signInManager)
member this.UserManager = userManager
member this.SignInManager = signInManager
[<AllowAnonymous>]
member this.Login() = this.View()
[<AllowAnonymous>]
member this.Register() = this.View()
[<HttpPost>]
[<AllowAnonymous>]
member this.Login (model:LoginViewModel) =
async {
let! result = Async.AwaitTask <| signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false)
match result with
| SignInStatus.Success ->
return true
| _ ->
ModelState.AddModelError("", "Invalid login attempt.")
return false
} |> Async.StartAsTask
[<HttpPost>]
[<AllowAnonymous>]
member this.Register (model:RegisterViewModel) =
async {
let user = ApplicationUser(UserName = model.Email, Email = model.Email)
let! result = Async.AwaitTask <| userManager.CreateAsync(user, model.Password)
if not result.Succeeded then
return false
else
do! Async.AwaitTask <| signInManager.SignInAsync(user, false, false)
return true
} |> Async.StartAsTask
namespace AwesomeAngularMVCApp.Models
open System.Web
open System.Web.Mvc
open System.ComponentModel.DataAnnotations
type LoginViewModel() =
[<Required>]
[<Display(Name = "Email")>]
[<EmailAddress>]
member val Email :string = null
[<Required>]
[<DataType(DataType.Password)>]
[<Display(Name = "Password")>]
member val Password :string = null
[<Display(Name = "Remember me?")>]
member val RememberMe = false
type RegisterViewModel() =
[<Required>]
[<EmailAddress>]
[<Display(Name = "Email")>]
member val Email :string = null
[<Required>]
[<StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)>]
[<DataType(DataType.Password)>]
[<Display(Name = "Password")>]
member val Password :string = null
[<DataType(DataType.Password)>]
[<Display(Name = "Confirm password")>]
[<Compare("Password", ErrorMessage = "The password and confirmation password do not match.")>]
member val ConfirmPassword :string = null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment