gpt4 book ai didi

asp.net - 使用 WCF 服务调用作为 ASP.NET Identity 的用户存储

转载 作者:行者123 更新时间:2023-12-02 12:18:00 25 4
gpt4 key购买 nike

我正在创建一个 Web 表单应用程序,它使用 WCF 服务与数据库和其他应用程序进行交互。此 Web 表单应用程序无法访问数据库。

我想使用 ASP.Net Identity 进行用户管理。我已经按照本教程 Overview of Custom Storage Providers for ASP.NET Identity 创建了自定义 UserStore 和 RoleStore ,如下图。

public class UserStore : IUserStore<IdentityUser, long>, IUserRoleStore<IdentityUser, long>
{
UserServiceClient userServiceClient = new UserServiceClient();

public Task CreateAsync(IdentityUser user)
{
string userName = HttpContext.Current.User.Identity.GetUserName();
Genders gender = (Genders)user.CoreUser.Gender.GenderId;

UserDto userDto = userServiceClient.CreateUser(user.CoreUser.FirstName, user.CoreUser.LastName, gender, user.CoreUser.EmailAddress, user.CoreUser.Username, userName, user.CoreUser.Msisdn);

return Task.FromResult<UserDto>(userDto);
}

public Task DeleteAsync(IdentityUser user)
{
bool success = userServiceClient.DeactivateUser(user.CoreUser.UserId, "");

return Task.FromResult<bool>(success);
}

public Task<IdentityUser> FindByIdAsync(long userId)
{
UserDto userDto = userServiceClient.GetUserByUserId(userId);

return Task.FromResult<IdentityUser>(new IdentityUser { CoreUser = userDto, UserName = userDto.Username });
}

public Task<IdentityUser> FindByNameAsync(string userName)
{
UserDto userDto = userServiceClient.GetUserByUsername(userName);

return Task.FromResult<IdentityUser>(new IdentityUser { CoreUser = userDto, UserName = userDto.Username });
}

public Task UpdateAsync(IdentityUser user)
{
Genders gender = (Genders)user.CoreUser.Gender.GenderId;

UserDto userDto = userServiceClient.UpdateUserDetails(user.CoreUser.UserId, user.CoreUser.FirstName, user.CoreUser.LastName, gender, user.CoreUser.EmailAddress, user.CoreUser.Msisdn, "");

return Task.FromResult<UserDto>(userDto);
}

public void Dispose()
{
throw new NotImplementedException();
}

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

public Task<IList<string>> GetRolesAsync(IdentityUser user)
{
List<UserRoleDto> roles = userServiceClient.GetUserRoles(user.Id);

return Task.FromResult<IList<string>>(roles.Select(r => r.Role.RoleName).ToList());
}

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

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

那是用户存储。现在的问题是为身份实现这一点。

public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

在上面使用模板预定义的类中,有这样一行:

app.CreatePerOwinContext(ApplicationDbContext.Create);

现在我没有 ApplicationDbContext,因为这是在 WCF 中处理的。另外,在 App_Start 文件夹中的 IdentityConfig 类中,有一个 Create 方法,其中包含这一行:

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

同样,我不知道用什么来替换 ApplicationDbContext。我这样做对吗?我遵循的教程足以帮助我满足我的需要吗?

最佳答案

我使用了这个链接,ASP.NET Identity 2.0 Extending Identity Models and Using Integer Keys Instead of Strings

问题更多在于我的用户 ID 是一个 long 而不是默认字符串。我也不需要传递上下文,因为我的 UserStore 并不期望其构造函数中有上下文

关于asp.net - 使用 WCF 服务调用作为 ASP.NET Identity 的用户存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33824656/

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