【发布时间】:2017-11-20 00:37:31
【问题描述】:
我遇到了由 ASP Identity 生成的上下文或 ApplicationDbContext 的问题,我在 IdentityConfig 中将其替换为 IdentityDbContext。调试时:
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<IdentityDbContext>()));
返回:
发生System.ArgumentNullException
重要文件:
IdentityDbContext.cs
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using TaskApplication.Models;
namespace TaskApplication.DAL
{
public class IdentityDbContext : DbContext
{
public IdentityDbContext()
: base("DefaultConnection")
{
}
public static IdentityDbContext Create()
{
return new IdentityDbContext();
}
}
}
IdentityConfig.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using TaskApplication.Models;
namespace TaskApplication
{
// Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
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<IdentityDbContext>()));
//var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(new IdentityDbContext("DefaultConnection")));
// 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,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// 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}"
});
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;
}
}
// Configure the application sign-in manager which is used in this application.
public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
}
public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
{
return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
}
}
}
顺便说一句。我在关注这个tutorial
【问题讨论】:
-
请只提供相关代码。 ViewModels 和 web.config 之类的东西与手头的错误无关。如果你得到
context.Get<IdentityDbContext>()的值,它会给你什么? -
我不知道如何检查它。调试时,在 Autos\Context 中给我 {Microsoft.Owin.OwinContext},在 Watch 1`context.Get
()` 中给我 null 值。 -
好吧。我自己解决了我的问题。我刚刚使用我的 Db Context 文件夹添加到 IdentityConfig.cs,并将
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<IdentityDbContext>()));更改为var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<DAL.IdentityDbContext>()));。在上一行代码中,IdentityDbContext 使用了错误的类,仅此而已。 -
您应该将其发布为答案。下次,不要使用已被引用包使用的名称。
标签: c# asp.net asp.net-mvc asp.net-identity