Oauth 2.0 WebAPI 2-OAuth 2拦截授权令牌

Oauth 2.0 WebAPI 2-OAuth 2拦截授权令牌,oauth-2.0,asp.net-web-api2,Oauth 2.0,Asp.net Web Api2,我使用托管在IIS上的owin创建了一个简单的web服务WebAPI 2 在我的启动文件中 public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 ConfigureOAuth(app);

我使用托管在IIS上的owin创建了一个简单的web服务WebAPI 2 在我的启动文件中

public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        ConfigureOAuth(app);
        var configuration = new HttpConfiguration();
        WebApiConfig.Register(configuration);
        app.UseWebApi(configuration);
        app.UseCors(CorsOptions.AllowAll);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

    }
并创建了一个具有authorize属性的简单控制器。在身份模型中,我添加了当用户调用服务器时要读取的信息

var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("sub", context.UserName));
        identity.AddClaim(new Claim("role", "user"));
        identity.AddClaim(new Claim("session", "50"));

对于我想要获取会话值的每一个请求,是否有一种方法可以做到这一点?如何添加中间件来拦截令牌授权过程?

您可以在控制器操作中尝试以下操作:

IPrincipal x = ControllerContext.RequestContext.Principal;
ClaimsIdentity ci = x.Identity as ClaimsIdentity;
string session = ci.FindFirst("session").Value;
可能重复的