我正在創建一個WebAPI(OData)項目並使用Asp.Identity來管理我的用戶。我已經通過Change Primary Key for Users in ASP.NET Identity閱讀,一切按規定運作,我不知道如何配置持票人令牌。在Tom FitzMacken的例子中,他如下配置Cookie認證。更改Asp.net身份和GetUserID的主鍵<int>
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) =>
user.GenerateUserIdentityAsync(manager),
getUserIdCallback:(id)=>(id.GetUserId<int>()))
}
});
下面是驗證碼,我有,從邁克·沃森的優秀文章採取Secure a Web API with Individual Accounts and Local Login in ASP.NET Web API 2.2
// 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<int>()