当getuserId函数不存在时,在控制器中获取当前userId

时间:2017-06-13 00:28:12

标签: asp.net-mvc asp.net-mvc-4 entity-framework-6 asp.net-identity

使用Aspnet Identity 2.0。我需要从HomeController访问当前登录的用户详细信息。我写的时候

User.Identity...

它只有以下选项

  • IsAuthenticated
  • AuthenticationType
  • 名称

没有GetUserId()函数,这是

类型的对象
  

System.Security.Principal.IIdentity IPrincipal.Identity

我项目的背景 我已将默认类重新安排到不同的项目中。

ABC.Web

  • AccountViewModels.cs
  • AccountController.cs
  • ApplicationUserMAnager

    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;
    }
    }
    

ABC.Entities

  • ApplicationUser
  • 作用
  • UserClaim
  • 用户登陆
  • 的UserRole

     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;
    }
    }
    

ABC.Core

  • ApplicationDbContext
  • USerStore
  • Rolestore的

0 个答案:

没有答案