【发布时间】:2015-01-09 18:14:22
【问题描述】:
我正在使用 Nuget 包 Microsoft.AspNet.Identity.EntityFramework 并连接到现有 SQL Server 数据库,其中 AspNetUsers 表 ID 列设置为 UniqueIdentifier。
在执行获取用户的调用时,我收到一个错误:
“IdentityUser`4”上的“Id”属性无法设置为“System.Guid”值。您必须将此属性设置为“System.String”类型的非空值。
有没有办法在代码中设置 Id 属性,因为我无法修改数据库上的列属性。
这是我的代码 sn-ps:
AuthProvider.cs
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task GrantCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using(Repository _repo = new Repository())
{
IdentityUser user = await _repo.FindUser(context.UName, context.PWord);
if(user == null)
{
// User Not Found / Invalid UName or PWord.
}
}
}
}
Repository.cs
public class Repository : IDisposable
{
private AppContext _ctx;
private UserManager<IdentityUser> _usrMgr;
public Repository()
{
_ctx = new AppContext();
_usrMgr = new UserManager<IdentityUser>(new UserStore<IdentityUser>(_ctx));
}
public async Task<IdentityUser> FindUser(string uName, string pWord)
{
// Setting the breakpoint here in this line below:
IdentityUser usr = await _usrMgr.FindAsync(uName, pWord);
return user;
}
}
在我在 Repository.cs 上设置的断点上,当我展开 _usrMgr 变量并检查用户属性时,我看到了错误。
更新: 我在这里找到了一些信息(在标题部分):
Make the Type of Primary Key Be Extensible for Users and Roles
但我不确定如何正确实施。我需要添加一个新课程吗?在我的理解中,那里的实现相当模糊。
【问题讨论】:
标签: c# asp.net-identity