本文介绍了更改Asp.net身份和GetUserID&LT主键,诠释>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的WebAPI(的OData)项目,并已使用Asp.Identity为我的用户管理。我已经通过更改主键在ASP.NET身份用户和一切工作为prescribed,我不知道如何配置承载令牌。在汤姆FitzMacken的榜样,他配置Cookie身份验证如下。

  app.UseCookieAuthentication(新CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LOGINPATH =新PathString(/帐号/登录),
    供应商=新CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator
            .OnValidateIdentity< ApplicationUserManager,ApplicationUser,INT>(
                validateInterval:TimeSpan.FromMinutes(30),
                regenerateIdentityCallback:(经理,用户)=>
                    user.GenerateUserIdentityAsync(经理)
                getUserIdCallback:(ID)=>(id.GetUserId< INT>()))
    }
});

下面是认证code,我有,从迈克·沃森的优秀文章采取的

  //配置基于流的OAuth应用
PublicClientId =自我;
OAuthOptions =新OAuthAuthorizationServerOptions {
    TokenEndpointPath =新PathString(/令牌),
    供应商=新ApplicationOAuthProvider(PublicClientId)
    AuthorizeEndpointPath =新PathString(/ API /帐号/ ExternalLogin),
    AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
    AllowInsecureHttp =真
};//启用应用程序使用承载令牌来验证用户身份
app.UseOAuthBearerTokens(OAuthOptions);

我认为我必须做的事情与OAuthAuthorizationServerProvider提供相当于getUserIdCallback,但我不知道有足够的了解它。

我的问题是,如何做app.UseOAuthBearerTokens(OAuthOptions)是一回事吗?


,我看到的现象是从我的控制器,GetUserId()始终返回0。

  User.Identity.GetUserId< INT>()


解决方案

这是我的错误。我试图打电话给User.Identity.GetUserID<从我的控制器>(),它总是返回0。事实证明,这个时候,我从.ctor调用它仅仅是真实的。当我使用GetUserInfo()从任何的方法,它将按预期工作。

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.

这篇关于更改Asp.net身份和GetUserID&LT主键,诠释&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 16:11