身份注册

时间:2019-03-16 14:12:07

标签: asp.net-mvc-4 asp.net-identity

我想在aspNetUser中添加自己的必填列,并修改身份控制器使其适合它,因此我首先添加了一个int列,并将其作为表参考书目的FK,而不是我在viewModel和register方法中进行的适配 这是我所做的尝试 在发布方法中,如果(ModelState.IsValid)我的模型无效,则列mybiblio无效,并且出现此错误 从类型'System.String'到类型''的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。

   [AllowAnonymous]
    public ActionResult Register()
    {
        ServiceReferenceBiblio.ServiceBiblioClient cls = new ServiceReferenceBiblio.ServiceBiblioClient();
        ViewBag.mybiblio =GetSelectListItems(cls.GetAll());
        return View();
    }

这是我的Register controller POST方法

public async Task<ActionResult> Register(RegisterViewModel mymodel, FormCollection form)
        {

            string selectedId = form["mybiblio"];
            string id = selectedId.Substring(0,1);
            int res;
            bool myidInteg = Int32.TryParse(id,out res);

            ServiceBiblioClient cls = new ServiceBiblioClient();

            foreach (var item in cls.GetAll())
            {
                if (item.id == res) {
                    mymodel.mybiblio = new clsBiblio { id = item.id, libellé = item.libellé};

                }
            }
            clsBiblio[] biblioArray = new clsBiblio[] { mymodel.mybiblio };
            ViewBag.mybiblio =GetSelectListItems(biblioArray);
            if (ModelState.IsValid)
            {

                var user = new ApplicationUser { UserName = mymodel.Email, Email = mymodel.Email };
                var result = await UserManager.CreateAsync(user, mymodel.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

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

            // If we got this far, something failed, redisplay form
            IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);

            return View(mymodel);
        }

并且我更改了RegisterViewModel:

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




        [Display(Name = "biblio")]
        public virtual clsBiblio mybiblio { get; set; }





        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

和我的applicationUser

public class ApplicationUser : IdentityUser
    {
        public virtual ICollection<clsBiblio> mybiblio { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("bibliothequeEntities", throwIfV1Schema: false)
        {
        }

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

        public System.Data.Entity.DbSet<LibraryOnWeb.Models.RegisterViewModel> RegisterViewModels { get; set; }

        public System.Data.Entity.DbSet<LibraryOnWeb.AspNetUser> AspNetUsers { get; set; }
        public System.Data.Entity.DbSet<clsBiblio> bibliotheque { get; set; }


    }

0 个答案:

没有答案
相关问题