更改Asp.net标识的主键和GetUserID

我正在创建一个WebAPI(OData)项目,并使用Asp.Identity进行用户管理。 我已经阅读了ASP.NET身份中用户的更改主键 ,一切都按规定工作,我不知道如何配置Bearer令牌。 在Tom FitzMacken的示例中,他按如下方式配置Cookie身份validation。

app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator .OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager), getUserIdCallback:(id)=>(id.GetUserId())) } }); 

这是我的身份validation代码,取自Mike Wasson的优秀文章, 在ASP.NET Web API 2.2中使用个人帐户和本地登录保护Web API

 // Configure the application for OAuth based flow PublicClientId = "self"; OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = new ApplicationOAuthProvider(PublicClientId), AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true }; // Enable the application to use bearer tokens to authenticate users app.UseOAuthBearerTokens(OAuthOptions); 

我假设我必须对OAuthAuthorizationServerProvider做一些事情以提供等效的“getUserIdCallback”,但我对此并不了解。

我的问题是,如何使用app.UseOAuthBearerTokens(OAuthOptions)做同样的事情?

[编辑]我看到的症状是来自我的控制器,GetUserId()总是返回0。

 User.Identity.GetUserId() 

这是我的错。 我试图从我的控制器调用User.Identity.GetUserID <>()并且它总是返回0.事实证明,只有当我从.ctor调用它时才会这样。 当我从任何方法使用GetUserInfo()时,它按预期工作。