简体   繁体   English

如何确定在调用动态javascript文件的页面与ASP.net中的用户之间

[英]How do I determine between a page calling a dynamic javascript file and a user in ASP.net

I'm making use of dynamic JavaScript files that are generated in asp.net and then embedded as a normal javascript in an html page. 我正在使用在asp.net中生成的动态JavaScript文件,然后将其作为普通javascript嵌入到html页面中。 Is there a way for me to determine if the dynamic javascript was opened directly or via the embedded script in the html page? 我是否可以确定动态javascript是直接打开还是通过html页面中的嵌入式脚本打开?

<script src="http://localhost:60919/js.ashx" type="text/javascript" charset="utf-8"></script>

If this was called from the web page, you should see the loading page listed in the referer property. 如果是从网页调用的,则应该看到Referer属性中列出的加载页面。

context.Request.UrlReferrer

OR 要么

context.Request.ServerVariables["HTTP_REFERER"]

This should be null if loaded directly by the user. 如果由用户直接加载, 则应为null。 However, a spammer/hacker can manually set the referer to gain access to the script independently of your page. 但是,垃圾邮件发送者/黑客可以手动设置引荐来源,以独立于您的页面访问脚本。

UPDATED: Handler.ashx: 更新:Handler.ashx:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/javascript";
        context.Response.Write("alert('"+context.Request.UrlReferrer+"');\n");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

Default.aspx: Default.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script src="Handler.ashx"></script>
    <h2>
        Welcome to ASP.NET!
    </h2>
    <p>
        To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
    </p>
    <p>
        You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
            title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
    </p>
</asp:Content>

Alert shows http://localhost:port/Default.aspx when loaded by the web page. 网页加载时,警报显示http:// localhost:port / Default.aspx When accessed directly, alert(''). 直接访问时,alert('')。

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

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