如何在ApplicationOAuthProvider asp.net web api2中使用Linq to Entities

时间:2017-05-31 12:33:49

标签: asp.net-mvc asp.net-web-api

此代码返回错误不支持linq实体,请帮帮我 错误返回是 LINQ to Entities无法识别方法'System.String get_Item(System.String)'方法,并且此方法无法转换为商店表达式。

这一行的问题         ApplicationUser user = dbContext.Users.Where(s => s.UserName == context.UserName&& s.Password == context.Password&& s.CodeID == data [“CodeID”]);

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
    private readonly string _publicClientId;

    public ApplicationOAuthProvider(string publicClientId)
    {
        if (publicClientId == null)
        {
            throw new ArgumentNullException("publicClientId");
        }

        _publicClientId = publicClientId;
    }

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
        var data = await context.Request.ReadFormAsync();

        ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);

        ApplicationDbContext dbContext = new ApplicationDbContext();

        //here problem
        //does not support linq to entities
        ApplicationUser user = dbContext.Users.Where(s => s.UserName == context.UserName &&  s.Password == context.Password && s.CodeID == data["CodeID"]);
        //here problem

        if (user == null)
        {
            context.SetError("invalid_grant", Resources.Resources.Invalid_UsernamePassword);
            return;
        }

        ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
           OAuthDefaults.AuthenticationType);
        ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
            CookieAuthenticationDefaults.AuthenticationType);

        AuthenticationProperties properties = CreateProperties(user.UserName);
        AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
        context.Validated(ticket);
        context.Request.Context.Authentication.SignIn(cookiesIdentity);
    }

    public override Task TokenEndpoint(OAuthTokenEndpointContext context)
    {
        foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
        {
            context.AdditionalResponseParameters.Add(property.Key, property.Value);
        }

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

    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        // Resource owner password credentials does not provide a client ID.
        if (context.ClientId == null)
        {
            context.Validated();
        }

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

    public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
    {
        if (context.ClientId == _publicClientId)
        {
            Uri expectedRootUri = new Uri(context.Request.Uri, "/");

            if (expectedRootUri.AbsoluteUri == context.RedirectUri)
            {
                context.Validated();
            }
        }

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

    public static AuthenticationProperties CreateProperties(string Username)
    {
        IDictionary<string, string> data = new Dictionary<string, string>
        {
            { "Username", Username }
        };
        return new AuthenticationProperties(data);
    }
}

1 个答案:

答案 0 :(得分:0)

Linq to entities无法转换此部分:

s.CodeID == data["CodeID"]

从数据集合中检索CodeID值到有意义的SQL语句中。

您需要做的是将此值存储在where表达式之前的变量中,如下所示:

var codeId = data["CodeID"];

然后修改您的where以使用它:

var codeId = data["CodeID"];
ApplicationUser user = dbContext.Users.Where(s => s.UserName == context.UserName &&  s.Password == context.Password && s.CodeID == codeId);

现在Linq可以正确生成SQL语句。