Asp.net mvc 当getuserId函数不存在时,在控制器中获取当前用户ID
使用Aspnet Identity 2.0。我需要从HomeController访问当前登录的用户详细信息。当我写作时Asp.net mvc 当getuserId函数不存在时,在控制器中获取当前用户ID,asp.net-mvc,asp.net-mvc-4,entity-framework-6,asp.net-identity,Asp.net Mvc,Asp.net Mvc 4,Entity Framework 6,Asp.net Identity,使用Aspnet Identity 2.0。我需要从HomeController访问当前登录的用户详细信息。当我写作时 User.Identity... 它只有以下选项 认证 身份验证类型 名字 没有GetUserId()函数,这是类型为的对象 系统、安全、主体、身份、主体身份 我的项目背景 我已经将默认类重新安排到不同的项目中 ABC.Web AccountViewModels.cs AccountController.cs 应用程序服务器管理器 public class Applic
User.Identity...
它只有以下选项
- 认证
- 身份验证类型
- 名字
- AccountViewModels.cs
- AccountController.cs
- 应用程序服务器管理器
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 UserStore<ApplicationUser, Role, int, UserLogin, UserRole, UserClaim>(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 = 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 in here. manager.RegisterTwoFactorProvider("PhoneCode", new PhoneNumberTokenProvider<ApplicationUser, int> { MessageFormat = "Your security code is: {0}" }); manager.RegisterTwoFactorProvider("EmailCode", 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; } }
公共类应用程序管理员:用户管理员 { 公共应用程序服务器管理器(IUserStore存储) :基地(商店) { } 公共静态应用程序SerManager创建(IdentityFactoryOptions选项,IOwinContext上下文) { var manager=newapplicationUserManager(newuserstore(context.Get()); //为用户名配置验证逻辑 manager.UserValidator=新的UserValidator(管理器) { AllowOnlyAlphanumericUserNames=false, RequireUniqueEmail=true }; //配置密码的验证逻辑 manager.PasswordValidator=新密码验证器 { 所需长度=6, RequiredOnletterDigit=真, RequireDigit=true, RequireLowercase=true, RequireUppercase=true, }; //注册双因素身份验证提供商。此应用程序使用电话和电子邮件作为接收验证用户代码的步骤 //您可以编写自己的提供程序并在此处插入。 manager.RegisterWofactorProvider(“PhoneCode”,新的PhoneNumberTokenProvider { MessageFormat=“您的安全代码为:{0}” }); manager.RegisterWofactorProvider(“EmailCode”,新的EmailTokenProvider { Subject=“安全代码”, BodyFormat=“您的安全代码为:{0}” }); manager.EmailService=新的EmailService(); manager.SmsService=新的SmsService(); var dataProtectionProvider=options.dataProtectionProvider; if(dataProtectionProvider!=null) { manager.UserTokenProvider=newdataprotectortokenprovider(dataProtectionProvider.Create(“ASP.NET标识”); } 退货经理; } }
- 应用程序用户
- 角色
- 用户声明
- 用户登录
- 用户角色
public class ApplicationUser : IdentityUser<int, UserLogin, UserRole, UserClaim> { public virtual User user { get; set; } 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; } }
公共类应用程序用户:IdentityUser { 公共虚拟用户用户{get;set;} 公共异步任务GenerateUserIdentityAsync(用户管理器) { //注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配 var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie); //在此处添加自定义用户声明 返回用户身份; } }
- 应用程序上下文
- 用户商店
- 罗莱斯托尔