[英]Why MVC Simple Membership token returns null when i want to register second user?
I dont know what the wrong is but I cant take membership the token for the second user. 我不知道这是怎么回事,但我不能为第二个用户获得令牌令牌。 I can easily register the first user but when i wanted to register second user, token comes null.
我可以轻松注册第一个用户,但是当我想注册第二个用户时,令牌为空。 I can get the user to the controller also WebSecurity.CreateUserAndAccount method gets values from the user.
我也可以将用户带到控制器,WebSecurity.CreateUserAndAccount方法也可以从用户获取值。 it is created in User Table of the database but not in dbo.webpages_Membership Table.
它是在数据库的用户表中创建的,而不是在dbo.webpages_Membership表中创建的。 I have deleted the database and migrated twice.
我已删除数据库并迁移了两次。 Eachtime I registered the first user without problem.
每次我注册第一个用户都没有问题。 But had problem when registering another user.
但是注册另一个用户时有问题。
Please see my codes; 请查看我的代码;
My Register View; 我的注册视图;
@using (Html.BeginForm("Register", "Admin", FormMethod.Post))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<form action="#">
<input type="text" name="Name" placeholder="Ad" />
<input type="email" name="Email" placeholder="E-posta Adresi" />
<input type="password" id="PasswordId" name="Password" placeholder="Şifre" />
<input type="password" id="PasswordConfirmId" name="PasswordConfirm" placeholder="Şifreni Onayla" /><label id="mismatch" style="visibility:hidden">Şifre uyuşmuyor</label>
<button type="submit" class="btn btn-default">Hesap Oluştur</button>
</form>
}
My Controller; 我的控制器;
public ActionResult Register()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(User user)
{
if (ModelState.IsValid)
{
try
{
string token = WebSecurity.CreateUserAndAccount(user.Email, user.Password, new { user.Password, user.PasswordConfirm, user.Email, user.Name }, true);
int userId = db.Users.FirstOrDefault(u => u.Email == user.Email).Id;
SendMail(user.Email, user.Password, user.Name, userId, token);
if (!Roles.RoleExists("User"))
Roles.CreateRole("User");
Roles.AddUserToRole(user.Email, "User");
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException ex)
{
ViewBag.Error = ex.Message;
return View(user);
}
}
return View(user);
}
I figured out that the problem is SQL Collation problem. 我发现问题是SQL排序规则问题。 CreateUserAndAccount Method try to upper the email "gmail" to "GMAİL".
CreateUserAndAccount方法尝试将电子邮件“ gmail”转换为“GMAİL”。 As there is no such a character in english, it returns error.
由于英语中没有这样的字符,因此会返回错误。
I have solved the problem adding one line code just before try statement and changed user.Email parameters with UpperEmail. 我已经解决了在try语句之前添加一行代码并使用UpperEmail更改了user.Email参数的问题。
string UpperEmail = user.Email.ToUpper().ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.