2016-01-20 82 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