gpt4 book ai didi

c# - 从服务创建用户管理器

转载 作者:行者123 更新时间:2023-11-30 18:45:30 26 4
gpt4 key购买 nike

我有一个如下所示的 UserService:

public class UserService : IUserStore<UserModel>, IUserLoginStore<UserModel>, IUserPasswordStore<UserModel>, IUserSecurityStampStore<UserModel>, IUserService
{
private readonly IUserRepository _userRepository;

public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
//create the user
}

我想从这个服务创建一个用户管理器。我创建了一个名为 IdentityStartup 的类,它看起来像这样:

public class IdentityStartup
{
public static Func<UserManager<UserModel>> UserManagerFactory { get; private set; }
private readonly IUserService _userService;
//public static Func<RoleManager<Role>> RoleManagerFactory { get; private set; }

public IdentityStartup(IUserService userService)
{
_userService = userService;
}


public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ApplicationCookie",
LoginPath = new PathString("/home/index")
});

// configure the user manager
UserManagerFactory = () =>
{
var usermanager = new UserManager<UserModel>(_userService);

// allow alphanumeric characters in username
usermanager.UserValidator = new UserValidator<UserModel>(new UserService())
{
AllowOnlyAlphanumericUserNames = false
};
return usermanager;
};

}
}

如您所见,我试图将 _userService 作为参数传递,但这不起作用,因为从 IUserService 转换为 Microsoft.AspNet.Identity.IUserStore 不起作用

我想将我的 UserService 用作 UserStore。

最佳答案

只做接口(interface)IUserService实现接口(interface) IUserStore<UserModel>

像这样:

public class UserService : IUserService
{
private readonly IUserRepository _userRepository;

public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
//create the user
}

public interface IUserService : IUserStore<UserModel>, IUserLoginStore<UserModel>, IUserPasswordStore<UserModel>, IUserSecurityStampStore<UserModel>
{
...
}

如果需要,您也可以使 IUserService 通用。

关于c# - 从服务创建用户管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49540843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com