【问题标题】:Web API OAUTH - Distinguish between if Identify Token Expired or UnAuthorizedWeb API OAUTH - 区分是否识别令牌已过期或未授权
【发布时间】:2015-11-17 18:48:56
【问题描述】:

目前正在使用 Owin、Oauth、Claims 开发授权服务器。

以下是我的 Oauth 配置,我有 2 个问题

 OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
  {

     AllowInsecureHttp = true,
     TokenEndpointPath = new PathString("/token"),
     AccessTokenExpireTimeSpan = TimeSpan.FromSeconds(1000),
     Provider = new AuthorizationServerProvider()
     //RefreshTokenProvider = new SimpleRefreshTokenProvider()
  };
     app.UseOAuthAuthorizationServer(OAuthServerOptions);
     app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

如果令牌已过期并且使用过期令牌访问的用户会收到 401(unAuthorized)。正在使用 Fiddler 进行检查。

如何向用户发送自定义消息,说明您的令牌已过期。我需要覆盖哪个功能或模块。

我的另一个问题是下面这行有什么用?

app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 我真的需要这个来实现吗,因为当我检查时它仍然可以在没有上述行的情况下工作。任何安全违规?

【问题讨论】:

    标签: oauth asp.net-web-api2 owin


    【解决方案1】:

    您不能直接自定义过期令牌的行为,但您可以使用自定义中间件来做到这一点。

    首先覆盖AuthenticationTokenProvider,以便您可以在身份验证票证过期之前拦截它。

    public class CustomAuthenticationTokenProvider : AuthenticationTokenProvider
    {
        public override void Receive(AuthenticationTokenReceiveContext context)
        {
            context.DeserializeTicket(context.Token);
    
            if (context.Ticket != null &&
                context.Ticket.Properties.ExpiresUtc.HasValue &&
                context.Ticket.Properties.ExpiresUtc.Value.LocalDateTime < DateTime.Now)
            {
                //store the expiration in the owin context so that we can read it later a middleware
                context.OwinContext.Set("custom.ExpriredToken", true);
            }
        }
    }
    

    并在启动时配置它以及一个小的自定义中间件

    using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, object>, System.Threading.Tasks.Task>;
    
    app.UseOAuthAuthorizationServer(OAuthServerOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
    {
        AccessTokenProvider = new CustomAuthenticationTokenProvider()
    });
    
    //after the request has been authenticated or not
    //check for our custom env setting and act accordingly
    app.Use(new Func<AppFunc, AppFunc>(next => (env) =>
    {
        var ctx = new OwinContext(env);
        if (ctx.Get<bool>("custom.ExpriredToken"))
        {
            //do wathever you want with the response
            ctx.Response.StatusCode = 401;
            ctx.Response.ReasonPhrase = "Token exprired";
    
            //terminate the request with this middleware
            return Task.FromResult(0);
        }
        else
        {
            //proceed with the rest of the middleware pipeline
            return next(env);
        }
    }));
    

    如果您注意到我在调用 UseOAuthBearerAuthentication 之后放置了自定义中间件,这很重要,并且源于您对第二个问题的回答。

    OAuthBearerAuthenticationMidlleware 负责认证但不负责授权。所以它只是读取令牌并填写信息,以便稍后在管道中使用IAuthenticationManager 访问它。

    所以是的,无论有没有它,您的所有请求都会以 401(未经授权)的形式出现,即使是那些具有有效令牌的请求。

    【讨论】:

    • 完美解决方案,为我节省大量时间!
    猜你喜欢
    • 2015-08-29
    • 2015-01-09
    • 2020-05-17
    • 2016-05-23
    • 2015-04-13
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2019-10-14
    相关资源
    最近更新 更多