简体   繁体   中英

Change Primary Key for Asp.net Identity and GetUserID<int>

I am creating a WebAPI (OData) project and have used Asp.Identity for my user management. I have read through Change Primary Key for Users in ASP.NET Identity and everything works as prescribed, I do not know how to configure the Bearer token. In Tom FitzMacken's example, he configures Cookie Authentication as follows.

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>()))
    } 
}); 

Here is the authentication code that I have, taken from Mike Wasson's excellent article, 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);

I assume that I have to do something with the OAuthAuthorizationServerProvider to provide the equivalent "getUserIdCallback", but I do not know enough about it.

My question is, how to do the same thing with app.UseOAuthBearerTokens(OAuthOptions)?

[Edit] The symptom that I am seeing is that from my controllers, GetUserId() always returns 0.

User.Identity.GetUserId<int>()

This was my mistake. I was trying to call User.Identity.GetUserID<>() from my controller and it always returned 0. It turns out that this is only true when I am calling it from the .ctor. When I use GetUserInfo() from any of the methods, it works as expected.

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