【问题标题】:Asp.Net Identity with custom UserStore, overload inaccessible带有自定义 UserStore 的 Asp.Net Identity,重载无法访问
【发布时间】:2023-03-23 03:35:01
【问题描述】:

我正在通过 RavenDB 实现 ASP.NET Identity 并尝试向我的用户添加角色。 https://github.com/ILMServices/RavenDB.AspNet.Identity/blob/master/RavenDB.AspNet.Identity/UserStore.cs

此实现具有以下可用重载public Task AddToRoleAsync(TUser user, string role)

ApplicationUserManager 派生自 UserManager 但我无法达到过载。唯一可用的方法是基本方法AddToRoleAsync(string UserId, string role)

Store 属性是 UserManager 基类中的公共属性,因此看起来并没有什么特别之处。

我也尝试直接调用manager.Store.AddToRoleAsync,但我再次没有通过智能感知恢复重载方法。

我希望有人能指引我正确的方向,真的卡在这里。

然后我的控制器当然继承了这个问题,这开始了我对为什么会发生这种情况的搜索。

针对 CodeNotFound,这里是 ApplicationUser。这很简单。

   public class ApplicationUser : IdentityUser, IEntity
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }


        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

            // Add custom user claims here
            return userIdentity;
        }
    }

【问题讨论】:

  • 可以给我们看看你的ApplicationUser的代码吗?
  • 我将课程添加到我的初始帖子中

标签: c# asp.net asp.net-mvc asp.net-identity ravendb


【解决方案1】:

这是因为您需要像这样在自定义 ApplicationUserManager 中编写

    /// <summary>
    /// Method to add user to multiple roles
    /// </summary>
    /// <param name="userId">user id</param>
    /// <param name="roles">list of role names</param>
    /// <returns/>
    public Task<IdentityResult> AddToRolesAsync(string userId, string[] roles)
    {
        return yourrolestore.AddToRolesAsync(userId, roles.GetStringArray());
    }

因为就像你写的那样,UserManager 不知道你定义的 UserStore。您必须手动完成链接

编辑

当你扩展 UserManager 时,你必须指定 Key 的类型。所以在你的情况下输入这个:

UserManager<ApplicationUser, int>

通常你会发现你的重载方法在参数中带有 int 键。

【讨论】:

  • 我知道为了将 UserStore 的重载暴露给我的控制器,我必须使用相同的重载扩展 ApplicationUserManager。但是,我仍然无法通过 ApplicationUserManager 中的扩展方法通过ApplicationUserMAnager.Store 达到 UserStore 中的重载。希望这有意义
猜你喜欢
  • 2014-06-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 2015-06-25
  • 2016-04-25
相关资源
最近更新 更多