As I said yesterday, Asp.Net Identity is something different!
I have created a data model based upon the default asp.net identity tables which is using the localdb.
You don't need this step to register a new User. The AspNetUserLogin table has an other purpose, the one consisting in saving external logins for the current user. So the user can login from Google, Facebook, etc.
To simply register a user, please drop AspNet**** tables from your model and write this code:
//GET
[AllowAnonymous]
public ActionResult RegisterNewUser()
{
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> RegisterNewUser(RegisterNewUserViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser
{
UserName = userViewModel.Email,
Email = userViewModel.Email,
EmailConfirmed =true
};
var adminresult = await UserManager.CreateAsync(user, userViewModel.Password);
//Add User to the Roles
string[] selectedRoles=new string[]{"Developer","Tester","Robbot"};
if (adminresult.Succeeded)
{
if (selectedRoles != null)
{
var result = await UserManager.AddToRolesAsync(user.Id, selectedRoles);
if (!result.Succeeded)
{
ModelState.AddModelError("", result.Errors.First());
return View();
}
}
}
else
{
ModelState.AddModelError("", adminresult.Errors.First());
return View();
}
return RedirectToAction("Index");
}
return View();
}
To use the AspNetUserLogin, You need two methods or steps:
The First method is the one that will request a redirect to the external login provider ExternalLogin for example, and
The Second method/step you need is the one that will save the External Login in the AspNetUserLogin table. this, without having this table generated in your model. Let's call this method ExternalLoginConfirmation.
Need code?
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
return RedirectToAction("Index", "Manage");
}
if (ModelState.IsValid)
{
var info = await AuthenticationManager.GetExternalLoginInfoAsync();
if (info == null)
{
return View("ExternalLoginFailure");
}
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user);
if (result.Succeeded)
{
//saving the External Login in the `AspNetUserLogin` table
result = await UserManager.AddLoginAsync(user.Id, info.Login);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToLocal("local url here");
}
}
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
ViewBag.ReturnUrl = returnUrl;
return View(model);
}
You'll need this class to!
internal class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUri)
: this(provider, redirectUri, null)
{
}
public ChallengeResult(string provider, string redirectUri, string userId)
{
LoginProvider = provider;
RedirectUri = redirectUri;
UserId = userId;
}
public string LoginProvider { get; set; }
public string RedirectUri { get; set; }
public string UserId { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XsrfKey] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
}
If you need more information or if you are facing issue with missing types, see this post
Hope this will help you...
Kind Regards!