2014-11-21 44 views
1

我有一個奇怪的問題,我發現這個網站上的幾個線程解決了我遇到的確切問題,但他們似乎都能夠通過在運行Createasync之前在applicationuser中設置安全戳來解決他們的問題方法和使用戶。在mvc項目中通過註冊頁面創建applicationuser時出現問題?

除了我沒有解決任何問題,我不斷收到一個異常,它說「值」參數不能爲空。

這裏是我的擴展applicationuser類:

public class ApplicationUser : IdentityUser 
    { 

     [Required] 
     [Display(Name = "Fornavn")] 
     [StringLength(50, MinimumLength = 0, ErrorMessage = "Titlen skal være på mellem 6 og 50 tegn")] 
     public string FirstName { get; set; } 

     [Required] 
     [Display(Name = "Efternavn")] 
     [StringLength(50, MinimumLength = 0, ErrorMessage = "Titlen skal være på mellem 6 og 50 tegn")] 
     public string LastName { get; set; } 

     [Required] 
     [Display(Name = "E-mail")] 
     [StringLength(50, MinimumLength = 0, ErrorMessage = "Titlen skal være på mellem 6 og 50 tegn")] 
     public string Email { get; set; } 

     [Display(Name = "Profilbillede")] 
     public String ProfileImagePath { get; set; } 

     [Display(Name = "Uddannelsessted")] 
     [StringLength(50, MinimumLength = 0, ErrorMessage = "Titlen skal være på mellem 6 og 50 tegn")] 
     public String Institution { get; set; } 

     [Display(Name = "")] 
     [StringLength(50, MinimumLength = 0, ErrorMessage = "Titlen skal være på mellem 6 og 50 tegn")] 
     public String Semester { get; set; } 

     [Display(Name = "Speciale")] 
     [StringLength(50, MinimumLength = 0, ErrorMessage = "Titlen skal være på mellem 6 og 50 tegn")] 
     public String Major { get; set; } 

     [Display(Name = "Niveau")] 
     public int? Level { get; set; } 

     [Display(Name = "Jobtitel")] 
     [StringLength(50, MinimumLength = 0, ErrorMessage = "Titlen skal være på mellem 6 og 50 tegn")] 
     public String Position { get; set; } 

     [Display(Name = "Kort beskrivelse")] 
     [StringLength(1000, MinimumLength = 100, ErrorMessage = "Titlen skal være på mellem 100 og 1000 tegn")] 
     public String Description { get; set; } 

     public int CurrentCase { get; set; } 

     public int CurrentExamSet { get; set; } 


    } 

而且這是在帳戶控制我的註冊方法:

[HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase image) 
     { 

      if (ModelState.IsValid) 
      { 

       var user = model.GetUser(); 
       if (image != null) 
       { 
        if (!Directory.Exists(Server.MapPath("~") + "/Images/ProfileImages")) 
         Directory.CreateDirectory(Server.MapPath("~") + "/Images/ProfileImages/"); 
        var guid = Guid.NewGuid().ToString(); 
        DataHelper.ResizeAndSave(Server.MapPath("~") + "/Images/ProfileImages/", guid, image.InputStream, 170); 
        user.ProfileImagePath = Server.MapPath("~") + "/Images/ProfileImages/" + guid + ".jpg"; 
       } 
       else 
       { 
        user.ProfileImagePath = ""; 
       } 
       user.SecurityStamp = Guid.NewGuid().ToString(); 
       var result = await UserManager.CreateAsync(user, model.Password);    

       if (result.Succeeded) 
       { 
        return RedirectToAction("Index", "Home"); 
       } 

      } 

      // If we got this far, something failed, redisplay form 

      return View(model); 

     } 

它崩潰這個確切的行:

var result = await UserManager.CreateAsync(user, model.Password); 

我已經嘗試設置每個字段值(即使在基類中),以便沒有e只要applicationuser類中的單個字段值爲null,但仍會彈出錯誤消息。

如果有人可以幫助我會很感激,因爲這個問題是嚴重的煩人,我不能進一步在我的項目。

堆棧跟蹤:

[ArgumentNullException: En værdi må ikke være null. 
Parameternavn: value] 
    System.ComponentModel.DataAnnotations.ValidationContext.set_DisplayName(String value) +48859 
    System.Data.Entity.Utilities.ValidationContextExtensions.SetDisplayName(ValidationContext validationContext, InternalMemberEntry property, DisplayAttribute displayAttribute) +274 
    System.Data.Entity.Internal.Validation.ValidationAttributeValidator.Validate(EntityValidationContext entityValidationContext, InternalMemberEntry property) +106 
    System.Data.Entity.Internal.Validation.PropertyValidator.Validate(EntityValidationContext entityValidationContext, InternalMemberEntry property) +148 
    System.Data.Entity.Internal.Validation.EntityValidator.ValidateProperties(EntityValidationContext entityValidationContext, InternalPropertyEntry parentProperty, List`1 validationErrors) +203 
    System.Data.Entity.Internal.Validation.TypeValidator.Validate(EntityValidationContext entityValidationContext, InternalPropertyEntry property) +105 
    System.Data.Entity.Internal.Validation.EntityValidator.Validate(EntityValidationContext entityValidationContext) +55 
    System.Data.Entity.Internal.InternalEntityEntry.GetValidationResult(IDictionary`2 items) +295 
    System.Data.Entity.DbContext.ValidateEntity(DbEntityEntry entityEntry, IDictionary`2 items) +86 
    Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`1.ValidateEntity(DbEntityEntry entityEntry, IDictionary`2 items) +2946 
    System.Data.Entity.DbContext.GetValidationErrors() +290 
    System.Data.Entity.Internal.InternalContext.SaveChangesAsync(CancellationToken cancellationToken) +143 
    System.Data.Entity.Internal.LazyInternalContext.SaveChangesAsync(CancellationToken cancellationToken) +66 
    System.Data.Entity.DbContext.SaveChangesAsync(CancellationToken cancellationToken) +60 
    System.Data.Entity.DbContext.SaveChangesAsync() +63 
    Microsoft.AspNet.Identity.EntityFramework.<SaveChanges>d__0.MoveNext() +148 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
    System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 
    Microsoft.AspNet.Identity.EntityFramework.<CreateAsync>d__5.MoveNext() +403 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
    System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 
    Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +984 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
    System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 
    Microsoft.AspNet.Identity.<CreateAsync>d__10.MoveNext() +883 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
    System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24 
    MEV3.Controllers.<Register>d__1a.MoveNext() in c:\Users\Little\Desktop\ASP.NET\MEV3\MEV3\Controllers\AccountController.cs:252 
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93 
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 
    System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21 
    lambda_method(Closure , Task) +64 
    System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +64 
    System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +114 
    System.Web.Mvc.Async.<>c__DisplayClass34.<BeginInvokeAsynchronousActionMethod>b__33(IAsyncResult asyncResult) +65 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49 
    System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3c() +117 
    System.Web.Mvc.Async.<>c__DisplayClass45.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3e() +323 
    System.Web.Mvc.Async.<>c__DisplayClass30.<BeginInvokeActionMethodWithFilters>b__2f(IAsyncResult asyncResult) +44 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50 
    System.Web.Mvc.Async.<>c__DisplayClass28.<BeginInvokeAction>b__19() +72 
    System.Web.Mvc.Async.<>c__DisplayClass1e.<BeginInvokeAction>b__1b(IAsyncResult asyncResult) +185 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 
    System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 
    System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +39 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 
    System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 
    System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651116 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

得到用戶的方法:

public ApplicationUser GetUser() 
     { 

      var user = new ApplicationUser() 

      { 

       UserName = this.UserName, 

       FirstName = this.FirstName, 

       LastName = this.LastName, 

       Email = this.Email, 

       ProfileImagePath = ProfileImagePath, 

       Semester = Semester, 

       Major = "", 

       Level = 1, 

       Institution = Institution, 

       Position = "Praktikant", 

       Description = Description, 

       CurrentCase = 0, 

       CurrentExamSet = 0 

      }; 
      return user; 

     } 

當我通過代碼,錯誤彈出它完成在accountcontroler此方法後,右:

protected override void Dispose(bool disposing) 
     { 
      if (disposing && UserManager != null) 
      { 
       UserManager.Dispose(); 
       UserManager = null; 
      } 
      base.Dispose(disposing); 
     } 
+0

Waht是你想將user.SecurityStamp設置爲自定義Guid的原因,因爲它是由身份框架自動生成並自動插入的? – DSR 2014-11-21 12:01:05

+0

因爲stackoverflow上的其他類似線程說這解決了這個問題。雖然沒有解決我的問題。不管我是否這樣,例外情況都是一樣的。 – ObedMarsh 2014-11-21 12:04:39

+0

你可以發佈你的GetUser()方法嗎? – tmg 2014-11-21 12:19:46

回答

2

它不是SecurityStamp,它的Email屬性驗證失敗。 IdentityUser已經有Email屬性,因此EmailApplicationUser屬性「隱藏」IdentityUser屬性。 刪除Email財產ApplicationUser解決問題。 IdentityUser已需要Email。您的本地化錯誤消息和標籤的其他註釋將它們放在您的RegisterViewModel上。

+0

我希望這能解決它,只有一個問題。當我從我的應用程序用戶中刪除值時,不會將電子郵件識別爲現有值。它顯示和錯誤像電子郵件不存在我的getuser方法了。我是否需要任何特殊語法來設置基類的電子郵件值? – ObedMarsh 2014-11-21 13:09:22

+0

我剛剛刪除完整的電子郵件值後,我仍然得到了同樣的該死的異常(是的,我更新了數據庫和所有):/ – ObedMarsh 2014-11-21 13:12:17

+0

它似乎工作,當我從應用程序用戶刪除所有驗證,sry忘了那部分在第一。儘管如此,我不認爲電子郵件的價值與它有任何關係。但你似乎是正確的驗證信息等不適用於應用程序用戶。那謝謝啦! :) – ObedMarsh 2014-11-21 13:16:43