public
Created

Membership service usage sample inside ASP.NET MVC controller

  • Download Gist
AccountController.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
public class AccountController : Controller
{
private readonly IMembershipService membershipService;
 
// service initialization is handled by IoC container
public AccountController(IMembershipService membershipService)
{
this.membershipService = membershipService;
}
 
// .. some other stuff ..
 
[HttpPost]
public ActionResult Register(RegisterModel model)
{
if (this.ModelSteate.IsValid)
{
// then passes it to the service method and checks for errors
// which returns ServiceResponse<User> with the status of operation and registered user entity
var response = this.membershipService.CreateUser(model);
 
if (response.Success)
{
FormsAuthentication.SetAuthCookie(response.Value.UserName, createPersistentCookie: false);
return RedirectToAction("Index", "Home");
}
 
response.Errors.CopyTo(this.ModelState);
}
 
return View();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.