将WFC服务调用用作ASP.NET身份的UserStore

时间:2015-11-20 10:43:34

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

我正在创建一个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();            
    }
}

那就是UserStore。现在问题是为Identity实现这一点。

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。我这样做了吗?我遵循的教程是否足以帮助我满足我的需求?

1 个答案:

答案 0 :(得分:0)

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

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