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

六月ゝ 毕业季﹏ 提交于 2020-01-14 03:45:07

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/27761310/change-primary-key-for-asp-net-identity-and-getuseridint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!