2015-11-20 62 views
2

我正在創建一個使用WCF服務與數據庫和其他應用程序進行交互的Web窗體應用程序。此Web表單應用程序無法訪問數據庫。使用WFC服務調用作爲UserStore for ASP.NET身份

我想使用ASP.Net身份進行用戶管理。我已經按照本教程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。現在問題是爲了實現這個標識。

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類,還有的方法創建了這條線,

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

再說,我有什麼替代ApplicationDbContext不知道。我做對了嗎?我遵循的教程是否足以幫助我滿足我的需求?

回答

相關問題