2016-01-20 77 views
0

我有我自己的自定义实体店用户和实体店角色。我现在正在尝试将用户添加到角色。通过这样做商店不实施IUserRoleStore <TUser>

public void CreateUser(RegisterBindingModel model, String roleName) 
    { 
     try 
     { 
      ApplicationRole role = null; 
      if (!UserRoleManager.RoleExists(roleName)) 
      { 
       role = new ApplicationRole() { Name = roleName, Id = Suid.NewSuid().ToString() }; 
       IdentityResult result = UserRoleManager.Create(role); 
      } 
      else 
      { 
       role = UserRoleManager.FindByName(roleName); 
      } 

      var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; 

      IdentityResult result1 = UserManager.Create(user, model.Password); 

      UserManager.AddToRole(user.Id, role.Id); 

     } 
     catch (Exception e) 
     { 
      throw new Exception(e.Message.ToString()); // I get an exception here saying, Store does not implement IUserRoleStore<TUser> 
     } 

    } 

之后,我实现了。我现在有这个功能

public Task<IList<string>> GetRolesAsync(TUser user) 
    { 

     throw new NotImplementedException(); 
    } 

我该如何实现它?我的意思是如何发送用户拥有的角色?我完整的实体用户存储情况如下

public class EntityUserStore<TUser, TAccount> : IUserStore<TUser>, IUserPasswordStore<TUser>, IUserEmailStore<TUser> , IUserRoleStore<TUser> 
    where TUser : class, IApplicationUser<TAccount>, new() 
    where TAccount : EntityModel, new() 
{ 
    public async Task CreateAsync(TUser user) 
    { 
     TableHelper.Save<TAccount>(user.Account); 
     EntityIndex.Set<TAccount>(user.UserName, user.Account); 
    } 

    public Task DeleteAsync(TUser user) 
    { 
     throw new NotImplementedException(); 
    } 

    public void Dispose() 
    { 

    } 

    public async Task<TUser> FindByIdAsync(string userId) 
    { 
     TAccount account = TableHelper.Get<TAccount>(userId); 

     if (account == null) 
      return null; 

     return new TUser() 
     { 
      Account = account 
     }; 

    } 

    public async Task<TUser> FindByNameAsync(string userName) 
    { 
     TAccount account = EntityIndex.Get<TAccount>(userName); 

     if (account == null) 
      return null; 

     return new TUser() 
     { 
      Account = account 
     }; 
    } 

    public Task UpdateAsync(TUser user) 
    { 
     throw new NotImplementedException(); 
    } 

    public async Task SetPasswordHashAsync(TUser user, string passwordHash) 
    { 
     user.PasswordHash = passwordHash; 

    } 

    public async Task<string> GetPasswordHashAsync(TUser user) 
    { 
     return user.PasswordHash; 
    } 

    public async Task<bool> HasPasswordAsync(TUser user) 
    { 
     return user.PasswordHash != null; 
    } 

    public async Task SetEmailAsync(TUser user, string email) 
    { 
     user.Email = email; 
    } 

    public async Task<string> GetEmailAsync(TUser user) 
    { 
     return user.Email; 
    } 

    public Task<bool> GetEmailConfirmedAsync(TUser user) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task SetEmailConfirmedAsync(TUser user, bool confirmed) 
    { 
     throw new NotImplementedException(); 
    } 

    public async Task<TUser> FindByEmailAsync(string email) 
    { 
     TAccount account = EntityIndex.Get<TAccount>(email); 

     if (account == null) 
      return null; 

     return new TUser() 
     { 
      Account = account 
     }; 
    } 

    public Task AddToRoleAsync(TUser user, string roleName) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task RemoveFromRoleAsync(TUser user, string roleName) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task<IList<string>> GetRolesAsync(TUser user) 
    { 

     throw new NotImplementedException(); 
    } 

    public Task<bool> IsInRoleAsync(TUser user, string roleName) 
    { 
     throw new NotImplementedException(); 
    } 
} 

我现在想这个

public Task<IList<string>> GetRolesAsync(TUser user) 
    { 
     List<string> roles = new UserManager<TUser>(this).GetRoles(user.Id).ToList(); 
     return Task.FromResult((IList<string>)roles); 
    } 

但它无法正常工作。我正在超时例外。

+0

请问,有没有你需要创建自己的实现的一个原因?我问,因为当我第一次使用ASP.NET Identity时,我开始做同样的事情。然后我意识到我可以使用框架中已经存在的实现,它的工作方式很轻松。 – Luke

+0

它是由我的老板完成的,但我认为原因是存储机制。我们不使用Sql,而是使用Azure。 – mohsinali1317

+0

无论您是否使用Azure,它仍然需要数据存储。如果它不是SQL,它是什么? – Luke

回答

0

您的EntityUserStoreGeneric classTUser不是类,它只是一种变量,将在编译时用类User类替换。

所以如果你想获得角色列表(特别是查看),你应该在你的控制器中获得真正的类。

+0

关于如何做到这一点的任何例子? – mohsinali1317

0

只需将实现以下接口IUserRoleStore<TUser>如下:

public class EntityUserStore<TUser, TAccount> : IUserStore<TUser>, 
IUserPasswordStore<TUser>, 
IUserEmailStore<TUser> , IUserRoleStore<TUser>, IUserRoleStore<TUser> 

这就强制你实现以下方法的具体实现;

public Task AddToRoleAsync(TUser user, string roleName) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task RemoveFromRoleAsync(TUser user, string roleName) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task<IList<string>> GetRolesAsync(TUser user) 
    { 
     throw new NotImplementedException(); 
    } 

    public Task<bool> IsInRoleAsync(TUser user, string roleName) 
    { 
     throw new NotImplementedException(); 
    } 

您只需针对您的数据源提供每种方法的实现。

的第一个的简化的例子可以是如下(取决于你的数据上下文等):

public Task AddToRoleAsync(TUser user, string roleName) 
    { 
     var roleToAdd = _context.Roles.FirstOrDefault(r => r.Name == roleName); 
     user.Roles.Add(roleToAdd); 
     return _context.SaveChangesAsync(); 
    } 

更新

在这里从斯图尔特韭菜leeksnet.AspNet.Identity.TableStorage发布例如这是你正在努力实现的一个例子。

看看这个类:UserStore.cs

+0

非常感谢。但我不确定在哪里获得_context。用户也没有角色。 – mohsinali1317

+0

因为我曾经在哪TUser:class,IApplicationUser ,new()然后IApplicationUser没有定义角色我现在已经添加了List 角色{get;组; }。所以角色问题消失了。但是我仍然遇到了_context的问题。这是从哪里来的。 – mohsinali1317

+1

你不会通过你的代码中的'TableHelper'来访问它吗? Roles属性可以通过'public virtual ICollection 角色{get; set;}' – hutchonoid