aspsessionid 名称更改

Gür*_*ala 5 javascript asp.net iis-7 asp-classic

<script type="text/javascript" language="javascript">
function setCookie()
{
    var path = '/', host = document.location.hostname;
    document.cookie = 'random=' + Math.floor(Math.random()*11) + '; path=' + path + ( host ? '; domain=' + document.location.hostname : "" ) ;
}
function readCookie()
{
    alert(document.cookie)
}
</script>
Run Code Online (Sandbox Code Playgroud)

如果我有一种简单的方法可以更改aspsessionid****为仅sessionid在我的日志中,我的生活会简单得多。有没有一种快速的方法来做到这一点......在Windows中?必须有一个脚本、批处理文件、命令或我可以在新日志文件上作为计划的日常任务运行的东西。在这种时候,我希望我能编程。欢迎提出建议!

小智 5

没有选项(已知或记录在案 - 可供公众使用)来更改 aspsessionids(经典 asp)的名称。

您可以从 IIS 或使用 @ENABLESESSIONSTATE 指令禁用会话(ASP -> 会话属性 -> 启用会话状态: false),然后继续使用由 asp(而不是 JavaScript)提供的您自己的 cookie。但只有当您的应用程序中不需要会话对象时,这才可以。

更好的方法是使用 Regex(Anthony W Jones 已经提供了 ASP 版本)或 .net(最小简化的 C# 示例)来更改日志文件中的这些“字符串”:

Regex rx = new Regex("ASPSESSIONID[A-Z]+=");

string log = rx.Replace(File.ReadAllText("u_ex120618.log"), "ASPSESSIONID=");
Console.WriteLine(log);
Run Code Online (Sandbox Code Playgroud)

有关 aspx 和 IIS 的更多信息

一种选择是使用处理程序来删除标头。

public class RemoveHttpHeadersModule : IHttpModule
{
    public RemoveHttpHeadersModule()
    {
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        if (context != null)
                context.PreSendRequestHeaders += this.OnPreSendRequestHeaders;
    }

    [SuppressMessage("Microsoft.Portability", "CA1903:UseOnlyApiFromTargetedFramework", MessageId = "System.Web.HttpResponse.#get_Headers()")]
    private void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        try
        {
            HttpContext.Current.Response.Headers.Remove("ETag");
            HttpContext.Current.Response.Headers.Remove("Server");
            HttpContext.Current.Response.Headers.Add("Server", "my server");
        }
        catch (HttpException)
        {
            throw;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另一种选择是控制 global.asax 中的所有内容(代码或编译库) - 涵盖您无权访问 IIS 管理器的情况。

删除(和/或添加)标头:

protected internal void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
    HttpContext.Current.Response.Headers.Remove("X-AspNet-Version");
    HttpContext.Current.Response.Headers.Remove("X-Powered-By");
    HttpContext.Current.Response.Headers.Remove("ETag");
    HttpContext.Current.Response.Headers.Remove("Server");
}
Run Code Online (Sandbox Code Playgroud)

处理错误

protected internal void Application_Error(object sender, EventArgs e)
{
    // get the error code            
    int ec = ((HttpException)HttpContext.Current.Error).GetHttpCode();
    // get the request path
    // string req = HttpContext.Current.Request.Path;
    // *** I suggest you to log the error before moving on
    // clear the error to avoid IIS actions
    HttpContext.Current.Server.ClearError();
    if (ec == 404)
    {
        // do what ever you want
    }
    // ... add other error codes handling;
}
Run Code Online (Sandbox Code Playgroud)

下一步是隐藏 aspx。

假设我们希望 .aspx 页面显示为 .html 这在这里得到解答:What is the theproper way to map .html to the ASP.NET pipeline in IIS7

只需注意选择正确的框架版本即可。如果您无权访问 IIS 管理器,请修改您的 web.config(仅提供此任务所需的内容):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="htmlpipe" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv4.0,bitness64" />
        </handlers>
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

上述设置可能会因您的 PC、服务器等而异。拥有具有相同基本属性(框架版本、32/64 位)的测试环境,在 IIS 中进行更改,然后检查 web.config 中生成的条目。

请允许我开个玩笑。“你喜欢这个产品?”

谢谢弗兰克,你插入了一些旧磁盘并找到了被遗忘的东西。很抱歉没有提供经典 ASP 的建议。

附言。不要忘记HackedByChinese的回答。


Hac*_*ese 1

如果您使用 .NET 2.0 或更高版本,您可以通过 web.config 更改 cookie 名称。

<configuration>
   <system.web>
      <sessionState cookieName="sessionid" />
   </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)