andrjfrks
andrjfrks

Reputation: 397

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. I have implemented custom 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. 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.

Upvotes: 1

Views: 422

Answers (1)

andrjfrks
andrjfrks

Reputation: 397

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

Upvotes: 1

Related Questions