繁体   English   中英

如何在设置HttpContext.Current.User.Identity之前检查OWIN请求?

[英]How do I check an OWIN request before HttpContext.Current.User.Identity is set?

我为使用Bearer令牌的webapi2网站设置了OWIN。 一切正常,但是我现在需要实施安全检查。

这是我需要完成的

  1. 在创建存储RemoteIpAddress的标识时添加新的声明。 我已经弄清楚了这部分。
  2. 检查使用承载令牌的每个传入请求,并检查当前ip地址是否与令牌中设置的IP地址匹配。 不知道哪里是最好的选择。

我打算将检查放入自定义AuthorizeAttribute中,但是感觉不合适。

是否可以使用OAuthAuthorizationServerProvider检查OWIN请求,并在不满足条件的情况下取消身份验证? 我真的不知道在哪里进行实际检查以验证/解密承载令牌,并且如果可能的话,我想知道是否可以使用钩子或覆盖。

您可以创建一个owin中间件,以检查Ip地址,这是该链中的第一步。 将其放置为附加到IAppBuilder的第一个中间件,以确保它将在链的其余部分之前调用

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.Use((context, next) =>
        {
            if (!IsIpAdressOk(context.Request)) //if the ip is invalid, stop the process and just return a response to the client
            {
                return context.Response.WriteAsync("Get lost!");
            }
            return Next.Invoke(context); //if the ip is correct then continue to the next middleware
        });

        ...add OAuthAuthorizationServerProvider and the rest here   
    }
}

约Owin中间件伟大的博客在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM