使用Sitecore CMS流水线处理器,我如何根据用户的IP地址重定向用户? [英] Using a Sitecore CMS pipeline processor, how do I redirect a user based on their IP address?

查看:141
本文介绍了使用Sitecore CMS流水线处理器,我如何根据用户的IP地址重定向用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用httpRequestBegin管道处理器执行此操作,但我似乎无法从给定的HttpRequestArgs参数访问用户的IP地址。



当我实现一个具有此方法的类时

pre $ public void Process(HttpRequestArgs args){
string ipAddress = args .Context.Request.UserHostAddress; //不工作
string state = GetState(ipAddress); //已在其他地方实现
RedirectUserByState(state); //已在其他地方实施
}

我认为这可能会使用户的IP地址

  args.Context.Request.UserHostAddress 

,但它会导致这个错误(栈跟踪表示它来自Process方法):

  System.Web.HttpException:请求在此上下文中不可用

任何想法?感谢!



编辑,这是在Sitecore 6.1和web.config中

 <管道> 
<! - --...-->
< httpRequestBegin>
<! - --...-->
< processor type =Sitecore.Pipelines.HttpRequest.ItemResolver,Sitecore.Kernel/>
< processor type =MySolution.Redirector,MySolution/>
< processor type =Sitecore.Pipelines.HttpRequest.LayoutResolver,Sitecore.Kernel/>
<! - --...-->
< / httpRequestBegin>
<! - --...-->
< / pipelinelines>

此服务器可能在负载平衡器后面。



编辑2:看起来像 试图在Process()函数中访问这两个,这就是导致错误的原因:

  Sitecore.Context.Request 
Sitecore.Context.Response
解决方案


解决方案

args.Context.Request 应该在请求处理的这一步中可用。最有可能的原因是这种处理器在Context没有可用的某些情况下被唤醒。一个简单的检查以下应该处理这些情况:

  if(args.Context!= null)
{
// ....
}

我能想到的唯一解释是 GetState() RedirectUserByState()正在调用 HttpContext.Current ,这在此时不可用(因此Sitecore为什么要将上下文作为参数传递)。

另外,负载均衡器不会解释异常,但是如果IP结果总是相同,你可能会有更好的运气检查下列服务器变量:

  args.Request。 ServerVariables [HTTP_X_FORWARDED_FOR] 
args.Request.ServerVariables [REMOTE_ADDR]


I am trying to do this with an httpRequestBegin pipeline processor, but I don't seem to be able to access the user's IP address from the given HttpRequestArgs parameter.

When I implement a class that has this method

public void Process(HttpRequestArgs args) {
    string ipAddress = args.Context.Request.UserHostAddress; // Not working
    string state = GetState(ipAddress); // already implemented elsewhere
    RedirectUserByState(state);  // already implemented elsewhere
}

I thought that this might hold the user's IP address

args.Context.Request.UserHostAddress

but it causes this error instead (stack trace says it originates from the Process method):

System.Web.HttpException: Request is not available in this context

Any ideas? Thanks!

Edit, this is in Sitecore 6.1 and in the web.config at

<pipelines>
<!--...-->
    <httpRequestBegin>
        <!--...-->
        <processor type="Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel"/>
        <processor type="MySolution.Redirector, MySolution"/>
        <processor type="Sitecore.Pipelines.HttpRequest.LayoutResolver, Sitecore.Kernel"/>
        <!--...-->
    </httpRequestBegin>
    <!--...-->
</pipelines>

This server might be behind a load balancer.

Edit 2: It looks like I was trying to access both of these inside the Process() function and that is what was causing the error:

Sitecore.Context.Request
Sitecore.Context.Response

解决方案

Where you defined your pipeline is fine. args.Context.Request should be available at this step in the request processing. The most likely cause is that this processor is being evoked under certain circumstances where the Context is not available. A simple check for the following should handle those cases:

if (args.Context != null)
{
    //....
}

The only other explanation I can think of is that GetState() or RedirectUserByState() are calling HttpContext.Current which is not available at this point (hence why Sitecore passes the context as an argument).

Also, a load-balancer would not explain the exceptions, but you may have better luck checking for the following server variables if the IP ends up always be the same:

args.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
args.Request.ServerVariables["REMOTE_ADDR"]

这篇关于使用Sitecore CMS流水线处理器,我如何根据用户的IP地址重定向用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆