[英]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. 我正在创建一个WebAPI(OData)项目,并已使用Asp.Identity进行用户管理。 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.
我已经阅读了ASP.NET Identity中的“更改用户的主键”,并且一切都按规定进行,但我不知道如何配置Bearer令牌。 In Tom FitzMacken's example, he configures Cookie Authentication as follows.
在Tom FitzMacken的示例中,他按以下方式配置Cookie身份验证。
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 这是我从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);
I assume that I have to do something with the OAuthAuthorizationServerProvider to provide the equivalent "getUserIdCallback", but I do not know enough about it. 我假设我必须对OAuthAuthorizationServerProvider做一些事情以提供等效的“ getUserIdCallback”,但是我对此并不了解。
My question is, how to do the same thing with app.UseOAuthBearerTokens(OAuthOptions)? 我的问题是,如何使用app.UseOAuthBearerTokens(OAuthOptions)做同样的事情?
[Edit] The symptom that I am seeing is that from my controllers, GetUserId() always returns 0. [编辑]我看到的症状是从我的控制器,GetUserId()始终返回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.
我试图从我的控制器调用User.Identity.GetUserID <>(),并且它始终返回0。事实证明,只有当我从.ctor调用它时,这才是正确的。 When I use GetUserInfo() from any of the methods, it works as expected.
当我从任何一种方法中使用GetUserInfo()时,它都能按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.