七牛的管理接口不支持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(从后端执行管理操作)

你可能感兴趣的:(七牛的管理接口不支持js端发送请求进行管理(设计到跨域问题))