简体   繁体   English

ASP.NET身份:自定义存储

[英]ASP.NET Identity: Custom stores

I'm trying to create ASP.NET MVC project with N-tier architecture and custom ASP.NET Identity service and repository. 我正在尝试使用N层体系结构以及自定义ASP.NET Identity服务和存储库创建ASP.NET MVC项目。 I have implemented custom UserStore 我已经实现了自定义UserStore

UserStore: UserIUserStore<User>, IUserPasswordStore<User>, IUserRoleStore<User>, IUserLoginStore<User>, IUserEmailStore<User> 

I've already implemented registration and login. 我已经实现了注册和登录。 Now I wanted to implement login via social networks and I've stucked. 现在,我想通过社交网络实现登录,但我一直坚持。

public class User : IUser<string>
{
    [Required]
    public string Id { get; set; }

    [Required]
    public string UserName { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    public string PasswordHash { get; set; }

    public virtual ICollection<Role> Roles { get; set; }

    public virtual ICollection<UserLoginInfo> Logins { get; set; }
}

UserLoginInfo is sealead class used in IUserLoginStore for login info. UserLoginInfo是在IUserLoginStore中用于获取登录信息的sealead类。 However if I wanted to add migration I got this error: 但是,如果我想添加迁移,则会出现此错误:

NtierMVC.Repositories.UserLoginInfo: : EntityType 'UserLoginInfo' has no key defined. Define the key for this EntityType.
Login: EntityType: EntitySet 'Login' is based on type 'UserLoginInfo' that has no keys defined.

I've tried to find any solution how to extend Login table with foreign key to user (like if you used default userstore) but not found none. 我尝试找到任何解决方案,如何使用用户的外键扩展登录表(例如,如果您使用默认用户存储),但找不到任何解决方案。 Any idea? 任何想法?

Thank you for your help. 谢谢您的帮助。

Well, I solved with a custom login class. 好吧,我通过自定义登录类解决了。

public class ExternalLogin
{
    public string UserId { get; set; }

    [Key, Column(Order = 0)]
    public string LoginProvider { get; set; }

    [Key, Column(Order = 1)]
    public string ProviderKey { get; set; }

    public UserLoginInfo GetUserLoginInfo()
    {
        return new UserLoginInfo(LoginProvider, ProviderKey);
    }
}

 public class User : IUser<string>
 {
    // user properties

    public virtual ICollection<ExternalLogin> Logins { get; set; }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM