Change Primary Key for Users in ASP.NET Identity
In Visual Studio 2013, the default web application uses a string value for the key for user accounts. ASP.NET Identity enables you to change the type of the key to meet your data requirements. For example, you can change the type of the key from a string to an integer.
This topic shows how to start with the default web application and change the user account key to an integer. You can use the same modifications to implement any type of key in your project. It shows how to make these changes in the default web application, but you could apply similar modifications to a customized application. It shows the changes needed when working with MVC or Web Forms.
Software versions used in the tutorial
- Visual Studio 2013 with Update 2 (or later)
- ASP.NET Identity 2.1 or later
To perform the steps in this tutorial, you must have Visual Studio 2013 Update 2 (or later), and a web application created from the ASP.NET Web Application template. The template changed in Update 3. This topic shows how to change the template in Update 2 and Update 3.
This topic contains the following sections:
- Change the type of the key in the Identity user class
- Add customized Identity classes that use the key type
- Change the context class and user manager to use the key type
- Change start-up configuration to use the key type
- For MVC with Update 2, change the AccountController to pass the key type
- For MVC with Update 3, change the AccountController and ManageController to pass the key type
- For Web Forms with Update 2, change Account pages to pass the key type
- For Web Forms with Update 3, change Account pages to pass the key type
- Run application
- Other resources
## Change the type of the key in the Identity user class
In your project created from the ASP.NET Web Application template, specify that the ApplicationUser class uses an integer for the key for user accounts. In IdentityModels.cs, change the ApplicationUser class to inherit from IdentityUser that has a type of int for the TKey generic parameter. You also pass the names of three customized class which you have not implemented yet.
[!code-csharpMain]
1: public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole,
2: CustomUserClaim>
3: {
4: ...
You have changed the type of the key, but, by default, the rest of the application still assumes the key is a string. You must explicitly indicate the type of the key in code that assumes a string.
In the ApplicationUser class, change the GenerateUserIdentityAsync method to include int, as shown in the highlighted code below. This change is not necessary for Web Forms projects with the Update 3 template.
[!code-csharpMain]
1: public async Task<ClaimsIdentity> GenerateUserIdentityAsync(
2: UserManager<ApplicationUser, int> manager)
3: {
4: // Note the authenticationType must match the one defined in
5: // CookieAuthenticationOptions.AuthenticationType
6: var userIdentity = await manager.CreateIdentityAsync(
7: this, DefaultAuthenticationTypes.ApplicationCookie);
8: // Add custom user claims here
9: return userIdentity;
10: }
## Add customized Identity classes that use the key type
The other Identity classes, such as IdentityUserRole, IdentityUserClaim, IdentityUserLogin, IdentityRole, UserStore, RoleStore, are still set up to use a string key. Create new versions of these classes that specify an integer for the key. You do not need to provide much implementation code in these classes, you are primarily just setting int as the key.
Add the following classes to your IdentityModels.cs file.
[!code-csharpMain]
1: public class CustomUserRole : IdentityUserRole<int> { }
2: public class CustomUserClaim : IdentityUserClaim<int> { }
3: public class CustomUserLogin : IdentityUserLogin<int> { }
4:
5: public class CustomRole : IdentityRole<int, CustomUserRole>
6: {
7: public CustomRole() { }
8: public CustomRole(string name) { Name = name; }
9: }
10:
11: public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
12: CustomUserLogin, CustomUserRole, CustomUserClaim>
13: {
14: public CustomUserStore(ApplicationDbContext context)
15: : base(context)
16: {
17: }
18: }
19:
20: public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
21: {
22: public CustomRoleStore(ApplicationDbContext context)
23: : base(context)
24: {
25: }
26: }
## Change the context class and user manager to use the key type
In IdentityModels.cs, change the definition of the ApplicationDbContext class to use your new customized classes and an int for the key, as shown in the highlighted code.
[!code-csharpMain]
1: public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
2: int, CustomUserLogin, CustomUserRole, CustomUserClaim>
3: {
4: ...
The ThrowIfV1Schema parameter is no longer valid in the constructor. Change the constructor so it does not pass a ThrowIfV1Schema value.
[!code-csharpMain]
1: public ApplicationDbContext()
2: : base("DefaultConnection")
3: {
4: }
Open IdentityConfig.cs, and change the ApplicationUserManger class to use your new user store class for persisting data and an int for the key.
[!code-csharpMain]
1: public class ApplicationUserManager : UserManager<ApplicationUser, int>
2: {
3: public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
4: : base(store)
5: {
6: }
7:
8: public static ApplicationUserManager Create(
9: IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
10: {
11: var manager = new ApplicationUserManager(
12: new CustomUserStore(context.Get<ApplicationDbContext>()));
13: // Configure validation logic for usernames
14: manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
15: {
16: AllowOnlyAlphanumericUserNames = false,
17: RequireUniqueEmail = true
18: };
19: // Configure validation logic for passwords
20: manager.PasswordValidator = new PasswordValidator
21: {
22: RequiredLength = 6,
23: RequireNonLetterOrDigit = true,
24: RequireDigit = true,
25: RequireLowercase = true,
26: RequireUppercase = true,
27: };
28: // Register two factor authentication providers. This application uses Phone
29: // and Emails as a step of receiving a code for verifying the user
30: // You can write your own provider and plug in here.
31: manager.RegisterTwoFactorProvider("PhoneCode",
32: new PhoneNumberTokenProvider<ApplicationUser, int>
33: {
34: MessageFormat = "Your security code is: {0}"
35: });
36: manager.RegisterTwoFactorProvider("EmailCode",
37: new EmailTokenProvider<ApplicationUser, int>
38: {
39: Subject = "Security Code",
40: BodyFormat = "Your security code is: {0}"
41: });
42: manager.EmailService = new EmailService();
43: manager.SmsService = new SmsService();
44: var dataProtectionProvider = options.DataProtectionProvider;
45: if (dataProtectionProvider != null)
46: {
47: manager.UserTokenProvider =
48: new DataProtectorTokenProvider<ApplicationUser, int>(
49: dataProtectionProvider.Create("ASP.NET Identity"));
50: }
51: return manager;
52: }
53: }
In the Update 3 template, you must change the ApplicationSignInManager class.
[!code-csharpMain]
1: public class ApplicationSignInManager : SignInManager<ApplicationUser, int>
2: { ... }
## Change start-up configuration to use the key type
In Startup.Auth.cs, replace the OnValidateIdentity code, as highlighted below. Notice that the getUserIdCallback definition, parses the string value into an integer.
[!code-csharpMain]
1: app.UseCookieAuthentication(new CookieAuthenticationOptions
2: {
3: AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
4: LoginPath = new PathString("/Account/Login"),
5: Provider = new CookieAuthenticationProvider
6: {
7: OnValidateIdentity = SecurityStampValidator
8: .OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
9: validateInterval: TimeSpan.FromMinutes(30),
10: regenerateIdentityCallback: (manager, user) =>
11: user.GenerateUserIdentityAsync(manager),
12: getUserIdCallback:(id)=>(id.GetUserId<int>()))
13: }
14: });
If your project does not recognize the generic implementation of the GetUserId method, you may need to update the ASP.NET Identity NuGet package to version 2.1
You have made a lot of changes to the infrastructure classes used by ASP.NET Identity. If you try compiling the project, you will notice a lot of errors. Fortunately, the remaining errors are all similar. The Identity class expects an integer for the key, but the controller (or Web Form) is passing a string value. In each case, you need to convert from a string to and integer by calling GetUserId<int>. You can either work through the error list from compilation or follow the changes below.
The remaining changes depend on the type of project you are creating and which update you have installed in Visual Studio. You can go directly to the relevant section through the following links
- For MVC with Update 2, change the AccountController to pass the key type
- For MVC with Update 3, change the AccountController and ManageController to pass the key type
- For Web Forms with Update 2, change Account pages to pass the key type
- For Web Forms with Update 3, change Account pages to pass the key type
## For MVC with Update 2, change the AccountController to pass the key type
Open the AccountController.cs file. You need to change the following methods.
ConfirmEmail method
[!code-csharpMain]
1: public async Task<ActionResult> ConfirmEmail(int userId, string code)
2: {
3: if (userId == default(int) || code == null)
4: {
5: return View("Error");
6: }
7:
8: IdentityResult result = await UserManager.ConfirmEmailAsync(userId, code);
9: if (result.Succeeded)
10: {
11: return View("ConfirmEmail");
12: }
13: else
14: {
15: AddErrors(result);
16: return View();
17: }
18: }
Disassociate method
[!code-csharpMain]
1: public async Task<ActionResult> Disassociate(string loginProvider, string providerKey)
2: {
3: ManageMessageId? message = null;
4: IdentityResult result = await UserManager.RemoveLoginAsync(
5: User.Identity.GetUserId<int>(),
6: new UserLoginInfo(loginProvider, providerKey));
7: if (result.Succeeded)
8: {
9: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
10: await SignInAsync(user, isPersistent: false);
11: message = ManageMessageId.RemoveLoginSuccess;
12: }
13: else
14: {
15: message = ManageMessageId.Error;
16: }
17: return RedirectToAction("Manage", new { Message = message });
18: }
Manage(ManageUserViewModel) method
[!code-csharpMain]
1: public async Task<ActionResult> Manage(ManageUserViewModel model)
2: {
3: bool hasPassword = HasPassword();
4: ViewBag.HasLocalPassword = hasPassword;
5: ViewBag.ReturnUrl = Url.Action("Manage");
6: if (hasPassword)
7: {
8: if (ModelState.IsValid)
9: {
10: IdentityResult result = await UserManager.ChangePasswordAsync(
11: User.Identity.GetUserId<int>(),
12: model.OldPassword,
13: model.NewPassword);
14: if (result.Succeeded)
15: {
16: var user = await UserManager.FindByIdAsync(
17: User.Identity.GetUserId<int>());
18: await SignInAsync(user, isPersistent: false);
19: return RedirectToAction("Manage", new {
20: Message = ManageMessageId.ChangePasswordSuccess });
21: }
22: else
23: {
24: AddErrors(result);
25: }
26: }
27: }
28: else
29: {
30: // User does not have a password so remove any validation errors caused
31: // by a missing OldPassword field
32: ModelState state = ModelState["OldPassword"];
33: if (state != null)
34: {
35: state.Errors.Clear();
36: }
37:
38: if (ModelState.IsValid)
39: {
40: IdentityResult result = await UserManager.AddPasswordAsync(
41: User.Identity.GetUserId<int>(), model.NewPassword);
42: if (result.Succeeded)
43: {
44: return RedirectToAction("Manage", new {
45: Message = ManageMessageId.SetPasswordSuccess });
46: }
47: else
48: {
49: AddErrors(result);
50: }
51: }
52: }
53:
54: // If we got this far, something failed, redisplay form
55: return View(model);
56: }
LinkLoginCallback method
[!code-csharpMain]
1: public async Task<ActionResult> LinkLoginCallback()
2: {
3: var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey,
4: User.Identity.GetUserId());
5: if (loginInfo == null)
6: {
7: return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
8: }
9: IdentityResult result = await UserManager.AddLoginAsync(
10: User.Identity.GetUserId<int>(), loginInfo.Login);
11: if (result.Succeeded)
12: {
13: return RedirectToAction("Manage");
14: }
15: return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
16: }
RemoveAccountList method
[!code-csharpMain]
1: public ActionResult RemoveAccountList()
2: {
3: var linkedAccounts = UserManager.GetLogins(User.Identity.GetUserId<int>());
4: ViewBag.ShowRemoveButton = HasPassword() || linkedAccounts.Count > 1;
5: return (ActionResult)PartialView("_RemoveAccountPartial", linkedAccounts);
6: }
HasPassword method
[!code-csharpMain]
1: private bool HasPassword()
2: {
3: var user = UserManager.FindById(User.Identity.GetUserId<int>());
4: if (user != null)
5: {
6: return user.PasswordHash != null;
7: }
8: return false;
9: }
You can now run the application and register a new user.
## For MVC with Update 3, change the AccountController and ManageController to pass the key type
Open the AccountController.cs file. You need to change the following method.
ConfirmEmail method
[!code-csharpMain]
1: public async Task<ActionResult> ConfirmEmail(int userId, string code)
2: {
3: if (userId == default(int) || code == null)
4: {
5: return View("Error");
6: }
7:
8: IdentityResult result = await UserManager.ConfirmEmailAsync(userId, code);
9: return View(result.Succeeded ? "ConfirmEmail" : "Error");
10: }
SendCode method
[!code-csharpMain]
1: public async Task<ActionResult> SendCode(string returnUrl, bool rememberMe)
2: {
3: var userId = await SignInManager.GetVerifiedUserIdAsync();
4: if (userId == default(int))
5: {
6: return View("Error");
7: }
8: var userFactors = await UserManager.GetValidTwoFactorProvidersAsync(userId);
9: var factorOptions = userFactors.Select(purpose => new SelectListItem { Text = purpose, Value = purpose }).ToList();
10: return View(new SendCodeViewModel { Providers = factorOptions, ReturnUrl = returnUrl, RememberMe = rememberMe });
11: }
Open the ManageController.cs file. You need to change the following methods.
Index method
[!code-csharpMain]
1: public async Task<ActionResult> Index(ManageMessageId? message)
2: {
3: ViewBag.StatusMessage =
4: message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
5: : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
6: : message == ManageMessageId.SetTwoFactorSuccess ? "Your two-factor authentication provider has been set."
7: : message == ManageMessageId.Error ? "An error has occurred."
8: : message == ManageMessageId.AddPhoneSuccess ? "Your phone number was added."
9: : message == ManageMessageId.RemovePhoneSuccess ? "Your phone number was removed."
10: : "";
11:
12: var model = new IndexViewModel
13: {
14: HasPassword = HasPassword(),
15: PhoneNumber = await UserManager.GetPhoneNumberAsync(User.Identity.GetUserId<int>()),
16: TwoFactor = await UserManager.GetTwoFactorEnabledAsync(User.Identity.GetUserId<int>()),
17: Logins = await UserManager.GetLoginsAsync(User.Identity.GetUserId<int>()),
18: BrowserRemembered = await AuthenticationManager.TwoFactorBrowserRememberedAsync(
19: User.Identity.GetUserId())
20: };
21: return View(model);
22: }
RemoveLogin methods
[!code-csharpMain]
1: public ActionResult RemoveLogin()
2: {
3: var linkedAccounts = UserManager.GetLogins((User.Identity.GetUserId<int>()));
4: ViewBag.ShowRemoveButton = HasPassword() || linkedAccounts.Count > 1;
5: return View(linkedAccounts);
6: }
7:
8: [HttpPost]
9: [ValidateAntiForgeryToken]
10: public async Task<ActionResult> RemoveLogin(string loginProvider, string providerKey)
11: {
12: ManageMessageId? message;
13: var result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId<int>(),
14: new UserLoginInfo(loginProvider, providerKey));
15: if (result.Succeeded)
16: {
17: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
18: if (user != null)
19: {
20: await SignInAsync(user, isPersistent: false);
21: }
22: message = ManageMessageId.RemoveLoginSuccess;
23: }
24: else
25: {
26: message = ManageMessageId.Error;
27: }
28: return RedirectToAction("ManageLogins", new { Message = message });
29: }
AddPhoneNumber method
[!code-csharpMain]
1: public async Task<ActionResult> AddPhoneNumber(AddPhoneNumberViewModel model)
2: {
3: if (!ModelState.IsValid)
4: {
5: return View(model);
6: }
7: // Generate the token and send it
8: var code = await UserManager.GenerateChangePhoneNumberTokenAsync(
9: User.Identity.GetUserId<int>(), model.Number);
10: if (UserManager.SmsService != null)
11: {
12: var message = new IdentityMessage
13: {
14: Destination = model.Number,
15: Body = "Your security code is: " + code
16: };
17: await UserManager.SmsService.SendAsync(message);
18: }
19: return RedirectToAction("VerifyPhoneNumber", new { PhoneNumber = model.Number });
20: }
EnableTwoFactorAuthentication method
[!code-csharpMain]
1: public async Task<ActionResult> EnableTwoFactorAuthentication()
2: {
3: await UserManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId<int>(), true);
4: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
5: if (user != null)
6: {
7: await SignInAsync(user, isPersistent: false);
8: }
9: return RedirectToAction("Index", "Manage");
10: }
DisableTwoFactorAuthentication method
[!code-csharpMain]
1: public async Task<ActionResult> DisableTwoFactorAuthentication()
2: {
3: await UserManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId<int>(), false);
4: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
5: if (user != null)
6: {
7: await SignInAsync(user, isPersistent: false);
8: }
9: return RedirectToAction("Index", "Manage");
10: }
VerifyPhoneNumber methods
[!code-csharpMain]
1: public async Task<ActionResult> VerifyPhoneNumber(string phoneNumber)
2: {
3: var code = await UserManager.GenerateChangePhoneNumberTokenAsync(
4: User.Identity.GetUserId<int>(), phoneNumber);
5: // Send an SMS through the SMS provider to verify the phone number
6: return phoneNumber == null ? View("Error") : View(new VerifyPhoneNumberViewModel { PhoneNumber = phoneNumber });
7: }
8:
9: [HttpPost]
10: [ValidateAntiForgeryToken]
11: public async Task<ActionResult> VerifyPhoneNumber(VerifyPhoneNumberViewModel model)
12: {
13: if (!ModelState.IsValid)
14: {
15: return View(model);
16: }
17: var result = await UserManager.ChangePhoneNumberAsync(
18: User.Identity.GetUserId<int>(), model.PhoneNumber, model.Code);
19: if (result.Succeeded)
20: {
21: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
22: if (user != null)
23: {
24: await SignInAsync(user, isPersistent: false);
25: }
26: return RedirectToAction("Index", new { Message = ManageMessageId.AddPhoneSuccess });
27: }
28: // If we got this far, something failed, redisplay form
29: ModelState.AddModelError("", "Failed to verify phone");
30: return View(model);
31: }
RemovePhoneNumber method
[!code-csharpMain]
1: public async Task<ActionResult> RemovePhoneNumber()
2: {
3: var result = await UserManager.SetPhoneNumberAsync(User.Identity.GetUserId<int>(), null);
4: if (!result.Succeeded)
5: {
6: return RedirectToAction("Index", new { Message = ManageMessageId.Error });
7: }
8: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
9: if (user != null)
10: {
11: await SignInAsync(user, isPersistent: false);
12: }
13: return RedirectToAction("Index", new { Message = ManageMessageId.RemovePhoneSuccess });
14: }
ChangePassword method
[!code-csharpMain]
1: [HttpPost]
2: [ValidateAntiForgeryToken]
3: public async Task<ActionResult> ChangePassword(ChangePasswordViewModel model)
4: {
5: if (!ModelState.IsValid)
6: {
7: return View(model);
8: }
9: var result = await UserManager.ChangePasswordAsync(
10: User.Identity.GetUserId<int>(), model.OldPassword, model.NewPassword);
11: if (result.Succeeded)
12: {
13: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
14: if (user != null)
15: {
16: await SignInAsync(user, isPersistent: false);
17: }
18: return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
19: }
20: AddErrors(result);
21: return View(model);
22: }
SetPassword method
[!code-csharpMain]
1: public async Task<ActionResult> SetPassword(SetPasswordViewModel model)
2: {
3: if (ModelState.IsValid)
4: {
5: var result = await UserManager.AddPasswordAsync(User.Identity.GetUserId<int>(), model.NewPassword);
6: if (result.Succeeded)
7: {
8: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
9: if (user != null)
10: {
11: await SignInAsync(user, isPersistent: false);
12: }
13: return RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess });
14: }
15: AddErrors(result);
16: }
17:
18: // If we got this far, something failed, redisplay form
19: return View(model);
20: }
ManageLogins method
[!code-csharpMain]
1: public async Task<ActionResult> ManageLogins(ManageMessageId? message)
2: {
3: ViewBag.StatusMessage =
4: message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
5: : message == ManageMessageId.Error ? "An error has occurred."
6: : "";
7: var user = await UserManager.FindByIdAsync(User.Identity.GetUserId<int>());
8: if (user == null)
9: {
10: return View("Error");
11: }
12: var userLogins = await UserManager.GetLoginsAsync(User.Identity.GetUserId<int>());
13: var otherLogins = AuthenticationManager.GetExternalAuthenticationTypes().Where(auth => userLogins.All(ul => auth.AuthenticationType != ul.LoginProvider)).ToList();
14: ViewBag.ShowRemoveButton = user.PasswordHash != null || userLogins.Count > 1;
15: return View(new ManageLoginsViewModel
16: {
17: CurrentLogins = userLogins,
18: OtherLogins = otherLogins
19: });
20: }
LinkLoginCallback method
[!code-csharpMain]
1: public async Task<ActionResult> LinkLoginCallback()
2: {
3: var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId());
4: if (loginInfo == null)
5: {
6: return RedirectToAction("ManageLogins", new { Message = ManageMessageId.Error });
7: }
8: var result = await UserManager.AddLoginAsync(User.Identity.GetUserId<int>(),
9: loginInfo.Login);
10: return result.Succeeded ? RedirectToAction("ManageLogins") :
11: RedirectToAction("ManageLogins", new { Message = ManageMessageId.Error });
12: }
HasPassword method
[!code-csharpMain]
1: private bool HasPassword()
2: {
3: var user = UserManager.FindById(User.Identity.GetUserId<int>());
4: if (user != null)
5: {
6: return user.PasswordHash != null;
7: }
8: return false;
9: }
HasPhoneNumber method
[!code-csharpMain]
1: private bool HasPhoneNumber()
2: {
3: var user = UserManager.FindById(User.Identity.GetUserId<int>());
4: if (user != null)
5: {
6: return user.PhoneNumber != null;
7: }
8: return false;
9: }
You can now run the application and register a new user.
## For Web Forms with Update 2, change Account pages to pass the key type
For Web Forms with Update 2, you need to change the following pages.
Confirm.aspx.cx
[!code-csharpMain]
1: protected void Page_Load(object sender, EventArgs e)
2: {
3: string code = IdentityHelper.GetCodeFromRequest(Request);
4: string userId = IdentityHelper.GetUserIdFromRequest(Request);
5: if (code != null && userId != null)
6: {
7: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
8: var result = manager.ConfirmEmail(Int32.Parse(userId), code);
9: if (result.Succeeded)
10: {
11: StatusMessage = "Thank you for confirming your account.";
12: return;
13: }
14: }
15:
16: StatusMessage = "An error has occurred";
17: }
RegisterExternalLogin.aspx.cs
[!code-csharpMain]
1: protected void Page_Load()
2: {
3: // Process the result from an auth provider in the request
4: ProviderName = IdentityHelper.GetProviderNameFromRequest(Request);
5: if (String.IsNullOrEmpty(ProviderName))
6: {
7: RedirectOnFail();
8: return;
9: }
10: if (!IsPostBack)
11: {
12: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
13: var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
14: if (loginInfo == null)
15: {
16: RedirectOnFail();
17: return;
18: }
19: var user = manager.Find(loginInfo.Login);
20: if (user != null)
21: {
22: IdentityHelper.SignIn(manager, user, isPersistent: false);
23: IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
24: }
25: else if (User.Identity.IsAuthenticated)
26: {
27: // Apply Xsrf check when linking
28: var verifiedloginInfo = Context.GetOwinContext().Authentication
29: .GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId());
30: if (verifiedloginInfo == null)
31: {
32: RedirectOnFail();
33: return;
34: }
35:
36: var result = manager.AddLogin(User.Identity.GetUserId<int>(),
37: verifiedloginInfo.Login);
38: if (result.Succeeded)
39: {
40: IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"],
41: Response);
42: }
43: else
44: {
45: AddErrors(result);
46: return;
47: }
48: }
49: else
50: {
51: email.Text = loginInfo.Email;
52: }
53: }
54: }
Manage.aspx.cs
[!code-csharpMain]
1: private bool HasPassword(ApplicationUserManager manager)
2: {
3: return manager.HasPassword(User.Identity.GetUserId<int>());
4: }
5:
6: protected void Page_Load()
7: {
8: if (!IsPostBack)
9: {
10: // Determine the sections to render
11: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
12: if (HasPassword(manager))
13: {
14: changePasswordHolder.Visible = true;
15: }
16: else
17: {
18: setPassword.Visible = true;
19: changePasswordHolder.Visible = false;
20: }
21: CanRemoveExternalLogins = manager.GetLogins(
22: User.Identity.GetUserId<int>()).Count() > 1;
23:
24: // Render success message
25: var message = Request.QueryString["m"];
26: if (message != null)
27: {
28: // Strip the query string from action
29: Form.Action = ResolveUrl("~/Account/Manage");
30:
31: SuccessMessage =
32: message == "ChangePwdSuccess" ? "Your password has been changed."
33: : message == "SetPwdSuccess" ? "Your password has been set."
34: : message == "RemoveLoginSuccess" ? "The account was removed."
35: : String.Empty;
36: successMessage.Visible = !String.IsNullOrEmpty(SuccessMessage);
37: }
38: }
39: }
40:
41: protected void ChangePassword_Click(object sender, EventArgs e)
42: {
43: if (IsValid)
44: {
45: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
46: IdentityResult result = manager.ChangePassword(
47: User.Identity.GetUserId<int>(),
48: CurrentPassword.Text,
49: NewPassword.Text);
50: if (result.Succeeded)
51: {
52: var user = manager.FindById(User.Identity.GetUserId<int>());
53: IdentityHelper.SignIn(manager, user, isPersistent: false);
54: Response.Redirect("~/Account/Manage?m=ChangePwdSuccess");
55: }
56: else
57: {
58: AddErrors(result);
59: }
60: }
61: }
62:
63: protected void SetPassword_Click(object sender, EventArgs e)
64: {
65: if (IsValid)
66: {
67: // Create the local login info and link the local account to the user
68: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
69: IdentityResult result = manager.AddPassword(User.Identity.GetUserId<int>(),
70: password.Text);
71: if (result.Succeeded)
72: {
73: Response.Redirect("~/Account/Manage?m=SetPwdSuccess");
74: }
75: else
76: {
77: AddErrors(result);
78: }
79: }
80: }
81:
82: public IEnumerable<UserLoginInfo> GetLogins()
83: {
84: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
85: var accounts = manager.GetLogins(User.Identity.GetUserId<int>());
86: CanRemoveExternalLogins = accounts.Count() > 1 || HasPassword(manager);
87: return accounts;
88: }
89:
90: public void RemoveLogin(string loginProvider, string providerKey)
91: {
92: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
93: var result = manager.RemoveLogin(User.Identity.GetUserId<int>(),
94: new UserLoginInfo(loginProvider, providerKey));
95: string msg = String.Empty;
96: if (result.Succeeded)
97: {
98: var user = manager.FindById(User.Identity.GetUserId<int>());
99: IdentityHelper.SignIn(manager, user, isPersistent: false);
100: msg = "?m=RemoveLoginSuccess";
101: }
102: Response.Redirect("~/Account/Manage" + msg);
103: }
You can now run the application and register a new user.
## For Web Forms with Update 3, change Account pages to pass the key type
For Web Forms with Update 3, you need to change the following pages.
Confirm.aspx.cx
[!code-csharpMain]
1: protected void Page_Load(object sender, EventArgs e)
2: {
3: string code = IdentityHelper.GetCodeFromRequest(Request);
4: string userId = IdentityHelper.GetUserIdFromRequest(Request);
5: if (code != null && userId != null)
6: {
7: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
8: var result = manager.ConfirmEmail(Int32.Parse(userId), code);
9: if (result.Succeeded)
10: {
11: StatusMessage = "Thank you for confirming your account.";
12: return;
13: }
14: }
15:
16: StatusMessage = "An error has occurred";
17: }
RegisterExternalLogin.aspx.cs
[!code-csharpMain]
1: protected void Page_Load()
2: {
3: // Process the result from an auth provider in the request
4: ProviderName = IdentityHelper.GetProviderNameFromRequest(Request);
5: if (String.IsNullOrEmpty(ProviderName))
6: {
7: RedirectOnFail();
8: return;
9: }
10: if (!IsPostBack)
11: {
12: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
13: var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
14: if (loginInfo == null)
15: {
16: RedirectOnFail();
17: return;
18: }
19: var user = manager.Find(loginInfo.Login);
20: if (user != null)
21: {
22: IdentityHelper.SignIn(manager, user, isPersistent: false);
23: IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
24: }
25: else if (User.Identity.IsAuthenticated)
26: {
27: // Apply Xsrf check when linking
28: var verifiedloginInfo = Context.GetOwinContext().Authentication
29: .GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId());
30: if (verifiedloginInfo == null)
31: {
32: RedirectOnFail();
33: return;
34: }
35:
36: var result = manager.AddLogin(User.Identity.GetUserId<int>(),
37: verifiedloginInfo.Login);
38: if (result.Succeeded)
39: {
40: IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"],
41: Response);
42: }
43: else
44: {
45: AddErrors(result);
46: return;
47: }
48: }
49: else
50: {
51: email.Text = loginInfo.Email;
52: }
53: }
54: }
Manage.aspx.cs
[!code-csharpMain]
1: public partial class Manage : System.Web.UI.Page
2: {
3: protected string SuccessMessage
4: {
5: get;
6: private set;
7: }
8:
9: private bool HasPassword(ApplicationUserManager manager)
10: {
11: return manager.HasPassword(User.Identity.GetUserId<int>());
12: }
13:
14: public bool HasPhoneNumber { get; private set; }
15:
16: public bool TwoFactorEnabled { get; private set; }
17:
18: public bool TwoFactorBrowserRemembered { get; private set; }
19:
20: public int LoginsCount { get; set; }
21:
22: protected void Page_Load()
23: {
24: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
25:
26: HasPhoneNumber = String.IsNullOrEmpty(manager.GetPhoneNumber(
27: User.Identity.GetUserId<int>()));
28:
29: // Enable this after setting up two-factor authentientication
30: //PhoneNumber.Text = manager.GetPhoneNumber(User.Identity.GetUserId()) ?? String.Empty;
31:
32: TwoFactorEnabled = manager.GetTwoFactorEnabled(User.Identity.GetUserId<int>());
33:
34: LoginsCount = manager.GetLogins(User.Identity.GetUserId<int>()).Count;
35:
36: var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
37:
38: if (!IsPostBack)
39: {
40: // Determine the sections to render
41: if (HasPassword(manager))
42: {
43: ChangePassword.Visible = true;
44: }
45: else
46: {
47: CreatePassword.Visible = true;
48: ChangePassword.Visible = false;
49: }
50:
51: // Render success message
52: var message = Request.QueryString["m"];
53: if (message != null)
54: {
55: // Strip the query string from action
56: Form.Action = ResolveUrl("~/Account/Manage");
57:
58: SuccessMessage =
59: message == "ChangePwdSuccess" ? "Your password has been changed."
60: : message == "SetPwdSuccess" ? "Your password has been set."
61: : message == "RemoveLoginSuccess" ? "The account was removed."
62: : message == "AddPhoneNumberSuccess" ? "Phone number has been added"
63: : message == "RemovePhoneNumberSuccess" ? "Phone number was removed"
64: : String.Empty;
65: successMessage.Visible = !String.IsNullOrEmpty(SuccessMessage);
66: }
67: }
68: }
69:
70: private void AddErrors(IdentityResult result)
71: {
72: foreach (var error in result.Errors)
73: {
74: ModelState.AddModelError("", error);
75: }
76: }
77:
78: // Remove phonenumber from user
79: protected void RemovePhone_Click(object sender, EventArgs e)
80: {
81: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
82: var result = manager.SetPhoneNumber(User.Identity.GetUserId<int>(), null);
83: if (!result.Succeeded)
84: {
85: return;
86: }
87: var user = manager.FindById(User.Identity.GetUserId<int>());
88: if (user != null)
89: {
90: IdentityHelper.SignIn(manager, user, isPersistent: false);
91: Response.Redirect("/Account/Manage?m=RemovePhoneNumberSuccess");
92: }
93: }
94:
95: // DisableTwoFactorAuthentication
96: protected void TwoFactorDisable_Click(object sender, EventArgs e)
97: {
98: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
99: manager.SetTwoFactorEnabled(User.Identity.GetUserId<int>(), false);
100:
101: Response.Redirect("/Account/Manage");
102: }
103:
104: //EnableTwoFactorAuthentication
105: protected void TwoFactorEnable_Click(object sender, EventArgs e)
106: {
107: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
108: manager.SetTwoFactorEnabled(User.Identity.GetUserId<int>(), true);
109:
110: Response.Redirect("/Account/Manage");
111: }
112: }
VerifyPhoneNumber.aspx.cs
[!code-csharpMain]
1: public partial class VerifyPhoneNumber : System.Web.UI.Page
2: {
3: protected void Page_Load(object sender, EventArgs e)
4: {
5: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
6: var phonenumber = Request.QueryString["PhoneNumber"];
7: var code = manager.GenerateChangePhoneNumberToken(
8: User.Identity.GetUserId<int>(), phonenumber);
9: PhoneNumber.Value = phonenumber;
10: }
11:
12: protected void Code_Click(object sender, EventArgs e)
13: {
14: if (!ModelState.IsValid)
15: {
16: ModelState.AddModelError("", "Invalid code");
17: return;
18: }
19:
20: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
21:
22: var result = manager.ChangePhoneNumber(
23: User.Identity.GetUserId<int>(), PhoneNumber.Value, Code.Text);
24:
25: if (result.Succeeded)
26: {
27: var user = manager.FindById(User.Identity.GetUserId<int>());
28:
29: if (user != null)
30: {
31: IdentityHelper.SignIn(manager, user, false);
32: Response.Redirect("/Account/Manage?m=AddPhoneNumberSuccess");
33: }
34: }
35:
36: // If we got this far, something failed, redisplay form
37: ModelState.AddModelError("", "Failed to verify phone");
38: }
39: }
AddPhoneNumber.aspx.cs
[!code-csharpMain]
1: public partial class AddPhoneNumber : System.Web.UI.Page
2: {
3: protected void PhoneNumber_Click(object sender, EventArgs e)
4: {
5: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
6: var code = manager.GenerateChangePhoneNumberToken(
7: User.Identity.GetUserId<int>(), PhoneNumber.Text);
8: if (manager.SmsService != null)
9: {
10: var message = new IdentityMessage
11: {
12: Destination = PhoneNumber.Text,
13: Body = "Your security code is " + code
14: };
15:
16: manager.SmsService.Send(message);
17: }
18:
19: Response.Redirect("/Account/VerifyPhoneNumber?PhoneNumber=" + HttpUtility.UrlEncode(PhoneNumber.Text));
20: }
21: }
ManagePassword.aspx.cs
[!code-csharpMain]
1: public partial class ManagePassword : System.Web.UI.Page
2: {
3: protected string SuccessMessage
4: {
5: get;
6: private set;
7: }
8:
9: private bool HasPassword(ApplicationUserManager manager)
10: {
11: return manager.HasPassword(User.Identity.GetUserId<int>());
12: }
13:
14: protected void Page_Load(object sender, EventArgs e)
15: {
16: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
17:
18: if (!IsPostBack)
19: {
20: // Determine the sections to render
21: if (HasPassword(manager))
22: {
23: changePasswordHolder.Visible = true;
24: }
25: else
26: {
27: setPassword.Visible = true;
28: changePasswordHolder.Visible = false;
29: }
30:
31: // Render success message
32: var message = Request.QueryString["m"];
33: if (message != null)
34: {
35: // Strip the query string from action
36: Form.Action = ResolveUrl("~/Account/Manage");
37: }
38: }
39: }
40:
41: protected void ChangePassword_Click(object sender, EventArgs e)
42: {
43: if (IsValid)
44: {
45: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
46: IdentityResult result = manager.ChangePassword(
47: User.Identity.GetUserId<int>(), CurrentPassword.Text, NewPassword.Text);
48: if (result.Succeeded)
49: {
50: var user = manager.FindById(User.Identity.GetUserId<int>());
51: IdentityHelper.SignIn(manager, user, isPersistent: false);
52: Response.Redirect("~/Account/Manage?m=ChangePwdSuccess");
53: }
54: else
55: {
56: AddErrors(result);
57: }
58: }
59: }
60:
61: protected void SetPassword_Click(object sender, EventArgs e)
62: {
63: if (IsValid)
64: {
65: // Create the local login info and link the local account to the user
66: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
67: IdentityResult result = manager.AddPassword(
68: User.Identity.GetUserId<int>(), password.Text);
69: if (result.Succeeded)
70: {
71: Response.Redirect("~/Account/Manage?m=SetPwdSuccess");
72: }
73: else
74: {
75: AddErrors(result);
76: }
77: }
78: }
79:
80: private void AddErrors(IdentityResult result)
81: {
82: foreach (var error in result.Errors)
83: {
84: ModelState.AddModelError("", error);
85: }
86: }
87: }
ManageLogins.aspx.cs
[!code-csharpMain]
1: public partial class ManageLogins : System.Web.UI.Page
2: {
3: protected string SuccessMessage
4: {
5: get;
6: private set;
7: }
8: protected bool CanRemoveExternalLogins
9: {
10: get;
11: private set;
12: }
13:
14: private bool HasPassword(ApplicationUserManager manager)
15: {
16: return manager.HasPassword(User.Identity.GetUserId<int>());
17: }
18:
19: protected void Page_Load(object sender, EventArgs e)
20: {
21: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
22: CanRemoveExternalLogins = manager.GetLogins(
23: User.Identity.GetUserId<int>()).Count() > 1;
24:
25: SuccessMessage = String.Empty;
26: successMessage.Visible = !String.IsNullOrEmpty(SuccessMessage);
27: }
28:
29: public IEnumerable<UserLoginInfo> GetLogins()
30: {
31: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
32: var accounts = manager.GetLogins(User.Identity.GetUserId<int>());
33: CanRemoveExternalLogins = accounts.Count() > 1 || HasPassword(manager);
34: return accounts;
35: }
36:
37: public void RemoveLogin(string loginProvider, string providerKey)
38: {
39: var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
40: var result = manager.RemoveLogin(
41: User.Identity.GetUserId<int>(), new UserLoginInfo(loginProvider, providerKey));
42: string msg = String.Empty;
43: if (result.Succeeded)
44: {
45: var user = manager.FindById(User.Identity.GetUserId<int>());
46: IdentityHelper.SignIn(manager, user, isPersistent: false);
47: msg = "?m=RemoveLoginSuccess";
48: }
49: Response.Redirect("~/Account/ManageLogins" + msg);
50: }
51: }
TwoFactorAuthenticationSignIn.aspx.cs
[!code-csharpMain]
1: public partial class TwoFactorAuthenticationSignIn : System.Web.UI.Page
2: {
3: private ApplicationSignInManager signinManager;
4: private ApplicationUserManager manager;
5:
6: public TwoFactorAuthenticationSignIn()
7: {
8: manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
9: signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
10: }
11:
12: protected void Page_Load(object sender, EventArgs e)
13: {
14: var userId = signinManager.GetVerifiedUserId<ApplicationUser, int>();
15: if (userId == default(int))
16: {
17: Response.Redirect("/Account/Error", true);
18: }
19: var userFactors = manager.GetValidTwoFactorProviders(userId);
20: Providers.DataSource = userFactors.Select(x => x).ToList();
21: Providers.DataBind();
22: }
23:
24: protected void CodeSubmit_Click(object sender, EventArgs e)
25: {
26: bool rememberMe = false;
27: bool.TryParse(Request.QueryString["RememberMe"], out rememberMe);
28:
29: var result = signinManager.TwoFactorSignIn<ApplicationUser, int>(SelectedProvider.Value, Code.Text, isPersistent: rememberMe, rememberBrowser: RememberBrowser.Checked);
30: switch (result)
31: {
32: case SignInStatus.Success:
33: IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
34: break;
35: case SignInStatus.LockedOut:
36: Response.Redirect("/Account/Lockout");
37: break;
38: case SignInStatus.Failure:
39: default:
40: FailureText.Text = "Invalid code";
41: ErrorMessage.Visible = true;
42: break;
43: }
44: }
45:
46: protected void ProviderSubmit_Click(object sender, EventArgs e)
47: {
48: if (!signinManager.SendTwoFactorCode(Providers.SelectedValue))
49: {
50: Response.Redirect("/Account/Error");
51: }
52:
53: var user = manager.FindById(signinManager.GetVerifiedUserId<ApplicationUser, int>());
54: if (user != null)
55: {
56: var code = manager.GenerateTwoFactorToken(user.Id, Providers.SelectedValue);
57: }
58:
59: SelectedProvider.Value = Providers.SelectedValue;
60: sendcode.Visible = false;
61: verifycode.Visible = true;
62: }
63: }
You have finished all of the required changes to the default Web Application template. Run the application and register a new user. After registering the user, you will notice that the AspNetUsers table has an Id column that is an integer.
If you have previously created the ASP.NET Identity tables with a different primary key, you need to make some additional changes. If possible, just delete the existing database. The database will be re-created with the correct design when you run the web application and add a new user. If deletion is not possible, run code first migrations to change the tables. However, the new integer primary key will not be set up as a SQL IDENTITY property in the database. You must manually set the Id column as an IDENTITY.
- Overview of Custom Storage Providers for ASP.NET Identity
- Migrating an Existing Website from SQL Membership to ASP.NET Identity
- Migrating Universal Provider Data for Membership and User Profiles to ASP.NET Identity
- Sample application with changed primary key
|