asp.net-mvc-5 – 在GenerateUserIdentityAsync方法的aspnet身份中找不到Use
|
注册用户后以及登录后我收到UserId未找到错误.因此,注册后,数据保存到数据库和dbo.AspNetUsers表中,id列自动递增,返回类型为int.
它之前成功运行但是现在它在这条线上给我错误— public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser,int> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
异常详细信息:System.InvalidOperationException:找不到UserId. 它早先工作正常,但现在当我加入其他表的外键关系时,我不知道那里缺少什么.在数据库中,所有表都是正确创建的,它们之间有适当的关系,但这里缺少一些东西. 我的ApplicationUser类是这样的——- public class ApplicationUser : IdentityUser<int,CustomUserLogin,CustomUserRole,CustomUserClaim>
{
public ApplicationUser()
{
this.Posts = new HashSet<Post>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser,DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomRole : IdentityRole<int,CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
public class CustomUserStore : UserStore<ApplicationUser,CustomRole,int,CustomUserClaim>
{
public CustomUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class CustomRoleStore : RoleStore<CustomRole,CustomUserRole>
{
public CustomRoleStore(ApplicationDbContext context)
: base(context)
{
}
}
我的IdentityConfig.cs类文件是这样的——- // 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,int>
{
public ApplicationUserManager(IUserStore<ApplicationUser,int> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,IOwinContext context)
{
var manager = new ApplicationUserManager(new CustomUserStore(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser,int>(manager)
{
AllowOnlyAlphanumericUserNames = false,RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 1,//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,int>
{
MessageFormat = "Your security code is {0}"
});
manager.RegisterTwoFactorProvider("Email Code",new EmailTokenProvider<ApplicationUser,int>
{
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,int>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}
// Configure the application sign-in manager which is used in this application.
public class ApplicationSignInManager : SignInManager<ApplicationUser,int>
{
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);
}
}
我已经看到很多stackoverflow的答案,但没有让它工作.可以有人plzz plzz看到缺少什么,我现在应该做什么.提前谢谢. 我在App_Start文件夹中的StartUp.Auth.cs是这样的—— public partial class Startup
{
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);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager,ApplicationUser,int>(
validateInterval: TimeSpan.FromMinutes(30),regenerateIdentityCallback: (manager,user) => user.GenerateUserIdentityAsync(manager),getUserIdCallback:(id)=>(id.GetUserId<int>()))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie,TimeSpan.FromMinutes(5));
// Enables the application to remember the second login verification factor such as phone or email.
// Once you check this option,your second step of verification during the login process will be remembered on the device where you logged in from.
// This is similar to the RememberMe option when you log in.
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
// Uncomment the following lines to enable logging in with third party login providers
//app.UseMicrosoftAccountAuthentication(
// clientId: "",// clientSecret: "");......................................................................
(编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-web-api – 将Application Insight与ASP API Core结
- asp.net – 实体框架删除子对象
- ASP.NET / IIS7 Url重写映射不起作用
- asp.net – AspNetCompatibilityRequirements是什么意思?
- asp.net – Panel visible = true没有任何效果
- asp.net-mvc – 如何创建ASP.NET MVC区域作为插件DLL?
- asp.net – 编码撇号
- asp.net-mvc-4 – Require.js优化vs asp.net mvc 4绑定和缩
- VS 2015预览缺少“ASP.NET 5 Web应用程序”项目类型?
- asp.net-mvc-3 – dataannotations在主键上设置标识种子值,
- iis-7 – 在iis7上设置经典的asp站点,站点运行但
- asp.net-mvc – Visual Studio不允许在MVC视图中
- asp.net-mvc-4 – 如何在一个Web应用程序中最好地
- asp.net-mvc – asp.net mvc中的加密视图状态
- asp.net-mvc – 在ASP.Net MVC视图中显示/隐藏链
- ASP.NET MVC通过ActionLink传递模型
- asp.net-mvc-3 – 后退按钮或导航到特定视图(页面
- asp.net-mvc – ASP.NET MVC中UpdateModel的正确
- asp.net-mvc – 在.NET MVC上启用Http PUT请求
- asp.net – html文本框的最大长度是多少?
