【发布时间】:2026-01-13 18:20:03
【问题描述】:
我想知道是否可以通过WCF service 管理用户。
例如,我的应用程序(我的 WCF 服务的客户端)调用函数 CreateUser(String login, string password, string email),我的服务使用 EF 身份和/或 ASP 成员身份创建此用户。
我已经安装了 nuget 包:OWIN/EF.Identity/ASP membership。
我已经尝试在我的 WCF 服务中创建类:
ApplicationDBContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("Scorpion_V1Users")
{
}
}
应用用户:
public class CustomApplicationUser : IdentityUser
{
public CustomApplicationUser()
{
this.Id = Guid.NewGuid().ToString();
}
public CustomApplicationUser(string userName): this()
{
UserName = userName;
}
}
我的自定义 UserStore:
public class CustomUserStore : IUserStore<CustomApplicationUser>, IUserPasswordStore<CustomApplicationUser>, IUserSecurityStampStore<CustomApplicationUser>
{
UserStore<IdentityUser> userStore = new UserStore<IdentityUser>(new CustomApplicationDbContext());
public CustomUserStore()
{
}
public Task CreateAsync(CustomApplicationUser user)
{
var context = userStore.Context as CustomApplicationDbContext;
context.Users.Add(user);
context.Configuration.ValidateOnSaveEnabled = false;
return context.SaveChangesAsync();
}
public void Create(CustomApplicationUser user)
{
var context = userStore.Context as CustomApplicationDbContext;
context.Users.Add(user);
context.Configuration.ValidateOnSaveEnabled = false;
context.SaveChanges();
}
public Task DeleteAsync(CustomApplicationUser user)
{
var context = userStore.Context as CustomApplicationDbContext;
context.Users.Remove(user);
context.Configuration.ValidateOnSaveEnabled = false;
return context.SaveChangesAsync();
}
public Task<CustomApplicationUser> FindByIdAsync(string userId)
{
var context = userStore.Context as CustomApplicationDbContext;
return context.Users.Where(u => u.Id.ToLower() == userId.ToLower()).FirstOrDefaultAsync();
}
public Task<CustomApplicationUser> FindByNameAsync(string userName)
{
var context = userStore.Context as CustomApplicationDbContext;
return context.Users.Where(u => u.UserName.ToLower() == userName.ToLower()).FirstOrDefaultAsync();
}
public Task UpdateAsync(CustomApplicationUser user)
{
var context = userStore.Context as CustomApplicationDbContext;
context.Users.Attach(user);
context.Entry(user).State = EntityState.Modified;
context.Configuration.ValidateOnSaveEnabled = false;
return context.SaveChangesAsync();
}
public void Dispose()
{
userStore.Dispose();
}
public Task<string> GetPasswordHashAsync(CustomApplicationUser user)
{
var identityUser = ToIdentityUser(user);
var task = userStore.GetPasswordHashAsync(identityUser);
SetApplicationUser(user, identityUser);
return task;
}
public Task<bool> HasPasswordAsync(CustomApplicationUser user)
{
var identityUser = ToIdentityUser(user);
var task = userStore.HasPasswordAsync(identityUser);
SetApplicationUser(user, identityUser);
return task;
}
public Task SetPasswordHashAsync(CustomApplicationUser user, string passwordHash)
{
var identityUser = ToIdentityUser(user);
var task = userStore.SetPasswordHashAsync(identityUser, passwordHash);
SetApplicationUser(user, identityUser);
return task;
}
public Task<string> GetSecurityStampAsync(CustomApplicationUser user)
{
var identityUser = ToIdentityUser(user);
var task = userStore.GetSecurityStampAsync(identityUser);
SetApplicationUser(user, identityUser);
return task;
}
public Task SetSecurityStampAsync(CustomApplicationUser user, string stamp)
{
var identityUser = ToIdentityUser(user);
var task = userStore.SetSecurityStampAsync(identityUser, stamp);
SetApplicationUser(user, identityUser);
return task;
}
private static void SetApplicationUser(CustomApplicationUser user, IdentityUser identityUser)
{
user.PasswordHash = identityUser.PasswordHash;
user.SecurityStamp = identityUser.SecurityStamp;
user.Id = identityUser.Id;
user.UserName = identityUser.UserName;
}
private IdentityUser ToIdentityUser(CustomApplicationUser user)
{
return new IdentityUser
{
Id = user.Id,
PasswordHash = user.PasswordHash,
SecurityStamp = user.SecurityStamp,
UserName = user.UserName
};
}
}
和我的 UserManager:
public class CustomUserManager
{
}
我已经在我的数据库上创建了所有 AspNet 表,并在我的 WCF 项目中添加了另一个 EDMX,指向它们。新创建的连接字符串是正确的(ScorpionV1Users)。
<add name="Scorpion_V1Users" connectionString="metadata=res://*/Database.Users.csdl|res://*/Database.Users.ssdl|res://*/Database.Users.msl;provider=System.Data.SqlClient;provider connection string="data source=TANTO;initial catalog=Scorpion_V1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
当我尝试时
private CustomUserStore _userStore = new CustomUserStore();
...
var user = new CustomApplicationUser { UserName = login, Email = mail };
_userStore.Create(user);
在用户商店中,
public void Create(CustomApplicationUser user)
{
var context = userStore.Context as CustomApplicationDbContext;
context.Users.Add(user);
context.Configuration.ValidateOnSaveEnabled = false;
context.SaveChanges();
}
一行
context.Users.Add(user);
抛出错误:'实体类型 CustomApplicationUser 不是当前上下文模型的一部分'。
我见过类似的其他问题,here 或 here,但它也不能解决我的问题。
我认为我创建 AspNetUsers 的模型不完整。 我还需要什么(类、nuget 包或其他 smt)来实现它?
提前致谢。
【问题讨论】:
标签: c# entity-framework wcf asp.net-identity