2015-01-04 19 views
1

我正在創建一個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>() 

回答

0

這是我的錯誤。我試圖從我的控制器調用User.Identity.GetUserID <>(),它總是返回0.原來,這隻有當我從.ctor調用它時纔是正確的。當我從任何方法使用GetUserInfo()時,它按預期工作。