Skip to content

Instantly share code, notes, and snippets.

Created April 19, 2017 00:03
Show Gist options
  • Save anonymous/c7f8ae86a144846bd5e41c39a7bd45fe to your computer and use it in GitHub Desktop.
Save anonymous/c7f8ae86a144846bd5e41c39a7bd45fe to your computer and use it in GitHub Desktop.
Get and Post of Registration Method
//GET
[AllowAnonymous]
public ActionResult Register(string message )
{
var myName = new SelectList(_context.Roles.ToList());
if (!Request.IsAuthenticated)
{
ViewBag.Message = message;
if (!User.IsInRole("Administrators"))
{
myName = new SelectList(_context.Roles.Where(x => x.Name == "Registered Users").ToList(), "Name", "Name");
}
else
{
myName = new SelectList(_context.Roles.ToList(), "Name", "Name");
}
}
else
{
ViewBag.Message = message;
if (User.IsInRole("Administrators"))
{
myName = new SelectList(_context.Roles.ToList(), "Name", "Name");
}
else
{
return RedirectToAction("Index", "Orders");
}
}
ViewBag.Name = myName;
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model, string message)
{
if (ModelState.IsValid)
{
if (!_context.Users.Any(x => x.UserName == model.UserName))
{
var user = new ApplicationUser { UserName = model.UserName };
user.Email = model.Email;
user.EmployeeId = model.EmployeeId;
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await this.UserManager.AddToRoleAsync(user.Id, model.Name);
string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Account confirmation");
if (User.IsInRole("Administrators"))
{
return RedirectToAction("Index", "User");
}
else
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
return RedirectToAction("UserLogin", "Account");
}
}
AddErrors(result);
}
else
{
if (User.IsInRole("Administrators"))
{
return RedirectToAction("Index", "User", new { message = "User already exists" });
}
else
{
return RedirectToAction("Register", "Account", new { message = "User already exists" });
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment