七牛的管理接口不支持js端发送请求进行管理(设计到跨域问题)

1,前端代码(举例)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="js-manage.aspx.cs" Inherits="WebApplication1.Up.js_manage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript">
        function qiniudelete() {
            //var pic = document.getElementById('bstext').value;
            var token = document.getElementById('TextBox2').value;
            var EncodedEntryURI = document.getElementById('TextBox1').value;
            var url = "http://rs.qiniu.com/delete/" + EncodedEntryURI;
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    document.getElementById("myDiv").innerHTML = xhr.responseText;
                }
            }
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.setRequestHeader("Authorization", " QBox " + token);
            xhr.send("gjjgjgj");
            alert(token);
            return false;
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
            <asp:Button ID="Button1" runat="server" Text="配置" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="return qiniudelete()" />
        </div>
        <div id="myDiv">
        </div>
    </form>
</body>
</html>

2,后端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.IO;

namespace WebApplication1.Up
{
    public partial class js_manage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            signning si = new signning();
            TextBox1.Text=  si.getBase64Encode(TextBox1.Text);
            string url = "http://rs.qiniu.com/delete/" + TextBox1.Text;
            TextBox2.Text = si.getAccessSign(url);


        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace WebApplication1.IO
{
    public class signning
    {
        String AK = "dL3iWMKzQMTap8Puxi5XcUgqzuKXHcIR";
        String SK = "fOW181hnBdkCOdM5Tpm7anPv0dwIBVvk1";
        String domen = "liuhanlin-work.qiniudn.com/";
        String NewURL = "";
        String sign = "";
        String operation = "";
        String FinalURL = "";

        public String Operation
        {
            get { return operation; }
            set { operation = value; }
        }

        public String getEncode(String bucket,String key)
        {
            String EncodedEntryURI = "";
            String entry = bucket+":"+key;
            Byte[] bytes = Encoding.Default.GetBytes(entry); 
            EncodedEntryURI = Convert.ToBase64String(bytes);
            System.Console.WriteLine("EncodedEntryURI"+EncodedEntryURI);
            return EncodedEntryURI;
        }
        public String getBase64Encode( String key)
        {
            String EncodedEntryURI = "";
            String entry = key;
            Byte[] bytes = Encoding.Default.GetBytes(entry);
            EncodedEntryURI = Convert.ToBase64String(bytes);
            EncodedEntryURI = EncodedEntryURI.Replace('+', '-').Replace('/', '_');
            //if (!EncodedEntryURI.Contains("="))
            //{
            //    EncodedEntryURI = EncodedEntryURI + "=";

            //}

            System.Console.WriteLine("EncodedEntryURI" + EncodedEntryURI);
            return EncodedEntryURI;
        }
        public String getSign(String bucket , String key)
        {
           NewURL = domen + key + Operation + "%7csaveas/" + getEncode(bucket, key);
           System.Text.Encoding encoding = System.Text.Encoding.UTF8;
           byte[] bytesNewURL = encoding.GetBytes(NewURL);
           byte[] bytesSK = encoding.GetBytes(SK);
           HMACSHA1 hmac = new HMACSHA1(bytesSK);
           byte[] digest = hmac.ComputeHash(bytesNewURL);
           sign =Convert.ToBase64String(digest);
           sign = sign.Replace('+', '-').Replace('/', '_');
           FinalURL = "http://"+NewURL + "/sign/" + AK + ":" + sign;
           return FinalURL;

        }
        public String getAccessSign(String s)
        {

            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            byte[] bytesNewURL = encoding.GetBytes(s);
            byte[] bytesSK = encoding.GetBytes(SK);
            HMACSHA1 hmac = new HMACSHA1(bytesSK);
            byte[] digest = hmac.ComputeHash(bytesNewURL);
            sign = Convert.ToBase64String(digest);
            sign = sign.Replace('+', '-').Replace('/', '_');
            FinalURL = AK + ":" + sign;
            return FinalURL;

        }


    }
}

3,用上面的方式去发送管理凭证就会出现跨域问题

因为我们的管理服务器不支持js跨域请求(以后也不会开放。)

3,唯一的解决方式使用我们的sdk(从后端执行管理操作)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值