【问题标题】:Use WFC service calls as UserStore for ASP.NET Identity使用 WCF 服务调用作为 ASP.NET 标识的用户存储
【发布时间】:2016-02-22 19:42:33
【问题描述】:

我正在创建一个使用 WCF 服务与数据库和其他应用程序交互的 Web 表单应用程序。此 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。我这样做对吗?我遵循的教程是否足以帮助我满足我的需求?

【问题讨论】:

  • 我有同样的场景,但我需要从 UserStore 为我的 Web 服务提供一个令牌,用于 FindByIdAsync。你是怎么做到的?您是否启用了 userServiceClient.GetUserByUsername(userName) 的匿名访问?

标签: asp.net wcf webforms asp.net-identity


【解决方案1】:

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

问题更多在于我的用户 ID 是 long 而不是默认字符串。我也不需要传递上下文,因为我的 UserStore 没想到它的构造函数中有上下文

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多