2017-10-05 74 views
1

試圖創建由asp.net身份提供的註冊頁面。我收到以下錯誤: System.ArgumentNullException:'值不能爲空。 參數名稱:經理'想在asp.net webform應用程序中使用asp.net身份創建註冊頁面

請幫我找出它爲什麼顯示爲空。任何幫助,高度讚賞。

我對register.aspx.cs代碼:

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>(); 
     var user = new ApplicationUser() { Email = EmailId.Text }; 
     IdentityResult result = manager.Create(user, Password.Text); 
     if (result.Succeeded) 
     { 
      signInManager.SignIn(user, isPersistent: false, rememberBrowser: false); 
      IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); 
     } 
     else 
     { 
      ErrorMessage.Text = result.Errors.FirstOrDefault(); 
     } 

而且我沒有做這部分的任何變化。我ApplicationUserManager代碼如下所示:

public class ApplicationUserManager : UserManager<ApplicationUser> 
    { 
     public ApplicationUserManager(IUserStore<ApplicationUser> store) 
      : base(store) 
     { 
     } 

     public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
     { 
      var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
      // Configure validation logic for usernames 
      manager.UserValidator = new UserValidator<ApplicationUser>(manager) 
      { 
       AllowOnlyAlphanumericUserNames = false, 
       RequireUniqueEmail = true 
      }; 

      // Configure validation logic for passwords 
      manager.PasswordValidator = new PasswordValidator 
      { 
       RequiredLength = 6, 
       RequireNonLetterOrDigit = true, 
       RequireDigit = true, 
       RequireLowercase = true, 
       RequireUppercase = true, 
      }; 

      // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user 
      // You can write your own provider and plug it in here. 
      manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser> 
      { 
       MessageFormat = "Your security code is {0}" 
      }); 
      manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser> 
      { 
       Subject = "Security Code", 
       BodyFormat = "Your security code is {0}" 
      }); 

      // Configure user lockout defaults 
      manager.UserLockoutEnabledByDefault = true; 
      manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); 
      manager.MaxFailedAccessAttemptsBeforeLockout = 5; 

      manager.EmailService = new EmailService(); 
      manager.SmsService = new SmsService(); 
      var dataProtectionProvider = options.DataProtectionProvider; 
      if (dataProtectionProvider != null) 
      { 
       manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity")); 
      } 
      return manager; 
     } 
    } 
+0

我dotn確切地知道你要去哪裏錯了。但這個鏈接對於學習身份是非常有用的http://benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1 –

+0

查看第2部分以及 –

+0

如果你可以把你的錯誤放在哪裏,它的更有幫助提供解決方案 –

回答

1

最有可能你缺少這一行

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 

Startup.Auth.cs文件。

在本位:

public void ConfigureAuth(IAppBuilder app) 
    { 
     // Configure the db context, user manager and signin manager to use a single instance per request 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); // <-- this is missing 
     // etc... 
+0

我應該在哪裏放置此代碼。 app.CreatePerOwinContext (ApplicationUserManager.Create);請指導我。謝謝 – Raj

+0

你有'Auth.Starup.cs'文件嗎? – trailmax

相關問題