简体   繁体   中英

WCF Data Services with URL in Query

I am working on setting up OAuth over WCF Data Services. I'm having an issues when trying to query the data source for the provider user key, since in some cases the key is an URL. For example, for google https://www.google.com/accounts/o8/id?id=AItOawnDT8v-6rdRI221piLFbOBT1m3EYTizmDQ

I have the following function :

public override int GetUserIdFromOAuth(string provider, string providerUserId)
{
    var encodedUserId = Uri.EscapeDataString(providerUserId);
    var user = service.OAuthMemberships
                      .Where(o => o.Provider == provider &&
                                  o.ProviderUserId == encodedUserId)
                      .SingleOrDefault();
    if (user == null)
        return -1;

    return user.UserId;
}

It works great for twitter since the ProviderUseId is just a number, but for google and yahoo where the UserId is a URL, I can't get it to match - it always comes up with 0 results even though I know the URLs are the same. I'm escaping the URL (or else the query would fail), but how can I get it to find the match like it should?

=====Edit

I know I can get it to work by querying on the provider first - do a ToList() and then query on the ProviderUserId - then it wouldn't have to be encoded since it wouldn't get sent over the DataServices. But I don't like the idea of pulling every record back for a single provider across the wire just as a workaround.

In order to move this along I set up a Service Operation to take care of this, but I'm still curious if there's a "real" answer to this question.

[WebGet]
public IEnumerable<OAuthMembership> GetOAuthUser(string provider, 
                                                 string providerUserId)
{
    providerUserId = Uri.UnescapeDataString(providerUserId);
    using (var db = new WebsiteMembershipEntities())
    {
        return db.OAuthMemberships.Where(o=>o.Provider == provider && 
                                            o.ProviderUserId == providerUserId)
                 .ToList();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM