我正在使用asp.net身份创建一个网站。我已经基于使用localdb的默认asp.net身份表创建了一个数据模型。当我尝试注册新用户时,我收到错误Bitev2.Models.AspNetUserLogin: : EntityType 'AspNetUserLogin' has no key defined. Define the key for this EntityType.
AspNetUserLogins: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNetUserLogin' that has no keys defined.
但是AspNetUserLogin模型是自动生成的并且不包含密钥。
非常感谢任何帮助。
AspNetUserLogin模型
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Bitev2.Models
{
using System;
using System.Collections.Generic;
public partial class AspNetUserLogin
{
public string UserId { get; set; }
public string LoginProvider { get; set; }
public string ProviderKey { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
}
}
AspNetLogins表/模型
答案 0 :(得分:0)
作为I said yesterday,Asp.Net Identity是不同的东西!
我已经基于使用localdb的默认asp.net身份表创建了一个数据模型。
您无需此步骤即可注册新用户。 AspNetUserLogin
表有另一个用途,一个用于保存当前用户的外部登录。因此用户可以从Google,Facebook等登录
要简单注册用户,请从模型中删除AspNet****
个表并编写此代码:
//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();
}
要使用AspNetUserLogin
,您需要两种方法或步骤:
第一种方法是请求重定向到外部登录提供程序ExternalLogin
的方法,例如
您需要的第二个方法/步骤是在AspNetUserLogin
表中保存外部登录的方法/步骤。这样,没有在您的模型中生成此表。我们称这个方法为ExternalLoginConfirmation
。
需要代码?
[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);
}
你需要这门课!
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);
}
}
如果您需要更多信息,或者您遇到缺少类型的问题,请see this post
希望这会对你有帮助......
亲切的问候!