【问题标题】:Sending custom http response code from GrantResourceOwnerCredentials从 GrantResourceOwnerCredentials 发送自定义 http 响应代码
【发布时间】:2016-10-02 17:44:51
【问题描述】:

当使用 ASP.NET WebAPI 2 OAuthAuthorizationServerProvider 时,可以在以下覆盖的方法中进行自定义凭据验证:

 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)

如果在执行此方法期间发生错误(例如 Db 连接错误),我想向请求者返回至少 500 个内部服务器错误。我似乎无法通过context.Response.StatusCode = 500; 设置自定义状态代码有没有办法通过这种方法控制响应代码?

【问题讨论】:

    标签: asp.net-web-api oauth owin provider


    【解决方案1】:

    Based on Greg P's original answer,有一些修改

    第 1 步:创建一个充当中间件的类

    using AppFunc = System.Func<System.Collections.Generic.IDictionary<string, System.Object>,
    System.Threading.Tasks.Task>;
    

    命名空间 SignOnAPI.Middleware.ResponseMiddleware {

    public class ResponseMiddleware 
    {
        AppFunc _next;
        ResponseMiddlewareOptions _options;
    
        public ResponseMiddleware(AppFunc nex, ResponseMiddlewareOptions options)
        {
            _next = next;
        }
    
        public async Task Invoke(IDictionary<string, object> environment)
        {
            var context = new OwinContext(environment);
    
            await _next(environment);
    
            if (context.Response.StatusCode == 400 && context.Response.Headers.ContainsKey("Change_Status_Code"))
            {
                //read the status code sent in the response
                var headerValues = context.Response.Headers.GetValues("Change_Status_Code");
    
                //replace the original status code with the new one
                context.Response.StatusCode = Convert.ToInt16(headerValues.FirstOrDefault());
    
                //remove the unnecessary header flag
                context.Response.Headers.Remove("Change_Status_Code");
            }
        }
    }
    

    Step2:创建扩展类(可省略)。

    这一步是可选的,可以修改为接受可以传递给中间件的选项。

    public static class ResponseMiddlewareExtensions
    {
        //method name that will be used in the startup class, add additional parameter to accept middleware options if necessary
        public static void UseResponseMiddleware(this IAppBuilder app)
        {
            app.Use<ResponseMiddleware>();
        }
    }
    

    第 3 步:在您的 OAuthAuthorizationServerProvider 实现中修改 GrantResourceOwnerCredentials 方法

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
    
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
    
            if (<database connection failed)
            {
                //first reject the context, to signify that the client is not valid
                context.Rejected();
    
                //set the error message
                context.SetError("invalid_username_or_password", "Invalid userName or password" );
    
                //add a new key in the header along with the statusCode you'd like to return
                context.Response.Headers.Add("Change_Status_Code", new[] { ((int)HttpStatusCode.InternalServerError).ToString() }); 
                return;
            }
        }
    

    Step4:在启动类中使用这个中间件

    public void Configuration(IAppBuilder app)
    {
        app.UseResponseMiddleware();
    
        //configure the authentication server provider
        ConfigureOAuth(app);
    
        //rest of your code goes here....
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-29
      • 2019-05-01
      • 1970-01-01
      • 2020-04-14
      • 2011-03-23
      • 1970-01-01
      相关资源
      最近更新 更多