3

我正在使用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模型是自動生成的,並且不包含任何密鑰。註冊用戶asp.net身份模型錯誤

任何幫助將不勝感激。

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表/模型

AspNetLogins table/model

+0

這種數據庫優先? – Shoe

+0

有沒有想到這一點?有同樣的問題... – Will

+0

試圖回答這個問題http://stackoverflow.com/questions/21785392/register-user-asp-net-identity-model-error/26423637#26423637 – Bellash

回答

0

由於I said yesterday,Asp.Net身份是不同的東西!

我已經創建了一個基於使用localdb的默認asp.net標識表的數據模型。

您不需要此步驟來註冊新的用戶。 AspNetUserLogin表還有另一個用途,即爲當前用戶保存外部登錄名。因此,用戶可以從谷歌,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

希望這將幫助你...

親切的問候!

相關問題