【问题标题】:Excel report unauthorized error linkExcel 报告未经授权的错误链接
【发布时间】:2018-01-18 06:09:54
【问题描述】:

我正在制作我的 SQL Server 表记录的 Excel 报告。在 Asp 页面网格视图中,我正在获取所需的数据,然后将这些数据下载到 excel 中。一列有超链接值,我需要这个超链接应该只在 Asp 页面网格视图中工作,但下载后,它应该重定向到一个新页面,其中将显示未经授权的访问错误。我没有得到如何在 Excel 文件超链接单击中显示未经授权的错误链接。
这是我的代码

protected void LnkBtnViewImage_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(cs);

        LinkButton lnkbtimage = sender as LinkButton;
        GridViewRow gvrowreport = lnkbtimage.NamingContainer as GridViewRow;
        //int Exhid = Convert.ToInt32(gvrowreport.Cells[1].Text);
        string Exhid = ((HiddenField)gvrowreport.Cells[0].FindControl("HiddenField1")).Value;
        SqlCommand cmd = new SqlCommand("select ImageName,ImageData from CompanyImage where Edition_Id='" + Session["Edition_ID"].ToString() + "' and Exhibitor_ID=@Exhibitor_ID ", con);
        cmd.Parameters.AddWithValue("@Exhibitor_ID", Exhid);
        //Select Statement con
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }

        SqlDataReader dr = cmd.ExecuteReader();


        if (dr!=null)
        {
            dr.Read();
            LinkButton lnkbtn = sender as LinkButton;
            GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
            //string filePath = GridViewLogo.DataKeys[gvrow.RowIndex].Value.ToString();
            //if (!Convert.IsDBNull(dr["ImageData"]))
            //{
                Response.ContentType = "application/vnd.ms-jpg";
                //to open file prompt Box open or Save file
                Response.AddHeader("content-disposition", "attachment;filename=" + dr["ImageName"].ToString());
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite((byte[])dr["ImageData"]);
                Response.End();
            //}
            //else
            //{
            //    //lblhid.Text = "Image is not uploaded here !!";
            //    //lblhid.ForeColor = Color.Green;
            //    //lblhid.Visible = true;
            //    //// lblhexcelerror.Visible = false;
            //    //gvrow.Visible = false;
            //}

        }

        else
        {
            //LinkButton lnkbtn = sender as LinkButton;
            //GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
            //gvrow.Visible = false;
        }

        con.Close(); 

【问题讨论】:

    标签: sql asp.net excel


    【解决方案1】:

    除了一些特定的例外情况(例如带有 CSRF 令牌的表单),通常情况下,当您单击链接时,服务器并不关心您是如何到达那里的,它会接收请求并提供请求的页面或资源。它并不真正知道链接是否包含在 Excel 文件、另一个 HTML 文档、电子邮件等中。事实上,这是超链接的聪明之处之一。

    即使您的建议是可行的,也没有什么可以阻止用户将其复制并粘贴到他们的浏览器中并从那里访问它。

    如果您不希望此链接在您的 Excel 文档中起作用,并且/或者您不希望向用户提供该链接,那么最简单的方法就是一开始就不将其包含在文档中。

    此外,如果此链接被提供给不应访问任何显示链接的用户,那么简单地不给他们链接(“默默无闻的安全性”)并不是真正的充分保护。如果是这种情况,那么您需要考虑如何授权访问该链接,无论用户从何处获得该链接的知识。

    【讨论】:

    • > 需要添加到excel文件中
    • @ibrahimshaikh 包含它的目的是什么,如果有人点击它,他们应该得到未经授权的错误?如果超链接不起作用,为什么要显示它?我不明白为什么有人会想要那个。
    【解决方案2】:

    在报告代码中使用修剪。 在修剪中,您必须在将其导出到 excel 时删除 "mailto:"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多