Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Identity 2 MySQL adding custom register Information

$
0
0

i try to get AspNet.Identity.MySQL to run with a MySQL storage Provider from the examlpe here:https://docs.microsoft.com/en-us/aspnet/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider

It also works so far as well. But now i try to expand the Register page with some additional informations, linke FirstName, LastName etc. At the beginning i tested it only with adding Firstname.

i changed the IdentityModel.cs as follwoing:

namespace IdentityMySQLDemo.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Beachten Sie, dass der "authenticationType" mit dem in "CookieAuthenticationOptions.AuthenticationType" definierten Typ übereinstimmen muss.
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Benutzerdefinierte Benutzeransprüche hier hinzufügen
            return userIdentity;

    }
        public string FirstName { get; set; }

    }

    public class ApplicationDbContext : MySQLDatabase
    {
        public ApplicationDbContext(string connectionName)
            : base(connectionName)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext("DefaultConnection");
        }
    }
}

adding this:

<div class="form-group">
        @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label"})<div class="col-md-10">
            @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control"})</div></div>

to the Register.cshtml

third i changed the RegisterViewModel at the AccountViewModels.cs

public class RegisterViewModel
        {
            [Required]
            [EmailAddress]
            [Display(Name = "E-Mail")]
            public string Email { get; set; }

            [Required]
            [StringLength(100, ErrorMessage = "\"{0}\" muss mindestens {2} Zeichen lang sein.", MinimumLength = 6)]
            [DataType(DataType.Password)]
            [Display(Name = "Kennwort")]
            public string Password { get; set; }

            [DataType(DataType.Password)]
            [Display(Name = "Kennwort bestätigen")]
            [Compare("Password", ErrorMessage = "Das Kennwort entspricht nicht dem Bestätigungskennwort.")]
            public string ConfirmPassword { get; set; }

            [Required]
            [DataType(DataType.Text, ErrorMessage = "Bitte geben Sie einen Vornamen ein")]
            [Display(Name = "Vorname")]
            public string FirstName { get; set; }
        }

and finally i changed Register function at the AccountController.cs:

public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

                user.FirstName = model.FirstName;

                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            return View(model);
        }

At my MySQL Database is also a column in the users table with the name: FirstName.

If i run this code the register page show me the new Firstname item. and i have also to fill out the textfield. After clicking the register link, it seems that everything goes well, but if i look into the users table the column FirstName has a value of NULL. So nothing was wrote to it.

I also tried to work with enable-migrations, update-database and so on...but nothing works.

Can someone tell me please where i did the mistake?

thanks in advance


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>