Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Roles changed #12

Open
chriship opened this issue Nov 5, 2015 · 2 comments
Open

Roles changed #12

chriship opened this issue Nov 5, 2015 · 2 comments

Comments

@chriship
Copy link

chriship commented Nov 5, 2015

Hi,

I have just implemented saas-scom into my project. I was using roles to differentiate between different user roles but it seems to have broken this.

In my AspNetUserRoles table, I now have a SaasEcomUser_Id field which is used by SaasEcomUser to get the role.

In my code, I set the role when someone registers using the following code:

var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);

var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.AddToRole(user.Id, "Manager");

Now, This sets the UserId of AspNetUserRoles instead of the SaasEcomUser_Id and therefore my roles no longer get picked up when logging in.

Is there are way to set the SaasEcomUser_Id using my method above?

Thanks!

@pedropaf
Copy link
Owner

Hi @chriship, not sure if you managed to fix this already, I've been having a look and I found out why this is happening.

If you look at how ASP.NET Identity 2 works, your application user has to inherit from IdentityUser. This class uses generics so you can specify which class types you want to use as Primary Key, Login, Roles and Claims:

public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
    where TLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
    where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{ ... }

At the moment the library SaasEcom.Core doesn't have the user SaasEcomUser implemented as a generic to map all those fields to the parent IdentityUser. It should be something similar to:

public abstract class SaasEcomUser : SaasEcomUser<string, IdentityUserRole>
{
}

public abstract class SaasEcomUser<TKey,TRole> : IdentityUser<TKey, IdentityUserLogin<TKey>,    TRole, IdentityUserClaim<TKey>> 
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
{ ... }

If you want to implement it and create a pull request I'm happy to merge it.

Thanks,
Pedro

@chriship
Copy link
Author

chriship commented Jan 1, 2016

Hi Pedro,

Thanks for the help. I had a look at implementing this but my generics knowledge is not up to scratch. Using your code above, I get an error on the GenerateUserIdentityAsync method:

The type 'Microsoft.AspNet.Identity.IUser<TKey>' cannot be used as type parameter 'TUser' in the 
generic type or method 'Microsoft.AspNet.Identity.UserManager<TUser>'. There is no implicit 
reference conversion from 'Microsoft.AspNet.Identity.IUser<TKey>' to 
'Microsoft.AspNet.Identity.IUser<string>'.

I had to change the SaasEcomUser parameter to include TKey and TRole:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<SaasEcomUser<TKey, TRole>> manager){...}

Any ideas what I am doing wrong here?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants