OwinContext。实体类型IdentityUser不是当前上下文模型的一部分

时间:2018-07-04 17:48:33

标签: c# entity-framework asp.net-web-api asp.net-identity

使用.NET Framework进行演示Web API并尝试使用JWT和Identity Framework实施自定义身份验证时出现错误,一切进展顺利。

我已经使用Identity进行了简单的身份验证,但是,当我实现 AspNetUsers 与我的模型 Person 之间的关系时,我遇到了一个例外*'实体类型IdentityUser不是当前上下文模型的一部分。'*

我认为这也可能与OwinContext有关,因为在调试一切正常时,它从表中获取用户数据,但是当执行下一个代码时,它仅返回异常(如上所述)。

我还认为它与其他问题有所不同,因为OwinContext是使用GetRoles方法时引发异常的那个。

var userRoles = context.OwinContext.Get<TwitterUserManager>().GetRoles(user.Id.ToString());

我的数据库上下文

public class FakeTwitterContext: IdentityDbContext<ApplicationUser>
{

    public FakeTwitterContext() : base("FakeTwitterConnection")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }

}

我的 ApplicationUser

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    //. . .

    public virtual ApplicationUser ApplicationUser { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Person Person { get; set; }

    public ApplicationUser(): base()
    {

    }
}

我的 PersonEntityConfig

public class PersonConfiguration : EntityTypeConfiguration<Person>
{
    public PersonConfiguration()
    {
        //. . .

        HasRequired(p => p.ApplicationUser)
            .WithRequiredDependent(appUser => appUser.Person)
            .Map(p => p.MapKey("UserId"));
    }
}

和我的 CustomOAuthProvider.cs (代码崩溃)

public class CustomOAuthProvider : OAuthAuthorizationServerProvider
{
    public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        var user = context.OwinContext.Get<FakeTwitterContext>()
            .Users.FirstOrDefault(u => u.UserName == context.UserName);

        if (!context.OwinContext.Get<TwitterUserManager>()
            .CheckPassword(user, context.Password)
            )
        {
            context.SetError("invalid_grant", "The user name or password is incorrect");
            context.Rejected();
            return Task.FromResult<object>(null);
        }

        var ticket = new AuthenticationTicket(SetClaimsIdentity(context, user), new AuthenticationProperties());
        context.Validated(ticket);

        return Task.FromResult<object>(null);
    }

    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated();
        return Task.FromResult<object>(null);
    }

    private static ClaimsIdentity SetClaimsIdentity(OAuthGrantResourceOwnerCredentialsContext context, ApplicationUser user)
    {
        var identity = new ClaimsIdentity("JWT");
        identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
        identity.AddClaim(new Claim("sub", context.UserName));

        //Error
        //The entity type IdentityUser is not part of the model for the current context.
        var userRoles = context.OwinContext.Get<TwitterUserManager>().GetRoles(user.Id.ToString());
        foreach (var role in userRoles)
        {
            identity.AddClaim(new Claim(ClaimTypes.Role, role));
        }

        return identity;
    }
}

0 个答案:

没有答案