2015-06-15 62 views
0

簡介:文件需要身份驗證時,文件夾已經驗證AJAX

我在IIS中的網站,它有一個名爲「集裝箱/文件」,這需要基本身份驗證來訪問文件夾,該文件夾的文件都會有一些8到9個用戶將通過我的網站下載的文件。另外我想在應用程序中存儲用戶名和密碼,所以不想看到用於身份驗證的窗口對話框。

我做了什麼:

我已經寫了,給了我訪問該文件夾,並使用我的用戶ID和密碼列出的所有文件Ajax請求,但是當我點擊要下載的文件,它再次要求我進行身份驗證。

我想實現:

如何繞過這個文件級別的身份驗證用戶已經通過身份驗證才能訪問這個文件夾,爲什麼他不能只是下載的文件?

我的代碼:

$(function() { 
 

 
    var id = "ss221", 
 
     pass = "test12", 
 
     url = "container/files/", 
 
     $s = $(".shell ul"); 
 

 
    // Setup our method to retrieve the file list 
 
    var getFiles = function() { 
 
     $.ajax({ 
 
      url: url, 
 
      type: 'GET', 
 
      headers: { 
 
       "Authorization": "Basic " + btoa(id + ":" + pass) 
 
      }, 
 
      success: function(data) { 
 
       alert('ok'); 
 
       var links = $(data).find("a"); 
 
       // for each item in links... 
 
       links.each(function(l) { 
 
        // extract the href attr 
 
        var href = $(this).attr("href"); 
 
        $s.append("<li><a target='_blank' href=\"" + href + "\">" + 
 
         $(this).text() + "</a></li>"); 
 
       }); 
 
      }, 
 
      error: function(XMLHttpRequest, textStatus, errorThrown) { 
 
       alert("error"); 
 
      } 
 
     }); 
 
    }; 
 
    // assign my click handler to #btnGet 
 
    $("#btnGet").click(function() { 
 

 
     $(".shell ul").empty(); 
 
     getFiles(); 
 
    }); 
 
});
.shell { 
 
    width: 300px; 
 
} 
 

 
.shell ul li { 
 
    list-style: none; 
 
} 
 

 
.shell a { 
 
    color: #232323; 
 
    display: block; 
 
    width: 100%; 
 
    height: 30px; 
 
    margin: .5em 0; 
 
    padding: 5px 10px; 
 
    font-weight: bold; 
 
    border: 1px solid #999; 
 
    border-radius: 3px; 
 
    text-decoration: none; 
 
}
<!doctype html> 
 
<html> 
 
<meta charset="utf-8"> 
 

 
<head> 
 
    <style type="text/css"> 
 

 
    </style> 
 
</head> 
 

 
<body> 
 
    <div> 
 
     List All files here... 
 
    </div> 
 
    <button id="btnGet"> 
 
     get</button> 
 
    <div class="shell"> 
 
     <!-- This is the place we're going to build our list --> 
 
     <ul> 
 
     </ul> 
 
    </div> 
 
</body> 
 
<!-- Get us some jQuery goodness! --> 
 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js"></script> 
 
<script type="text/javascript"> 
 
</script> 
 

 
</html>

回答

0

通過更新上面的代碼,並呼籲所有文件阿賈克斯承諾也解決了這個問題,所以,每當文件夾通過認證並顯示所有文件,點擊文件將它們與同creds驗證它也會打開文件。

$(function() { 
 

 
    var id = "ss221", 
 
    pass = "test12", 
 
    $s = $(".shell ul"); 
 

 
    // Setup our method to retrieve the file list 
 
    var getAuthenticated = function(url) { 
 
    return $.ajax({ 
 
     url: url, 
 
     type: 'GET', 
 
     headers: { 
 
     "Authorization": "Basic " + btoa(id + ":" + pass) 
 
     } 
 
    }); 
 
    }; 
 
    $(".shell ul").empty(); 
 
    var promise = getAuthenticated("files/"); 
 
    promise.success(function(data) { 
 

 
    var links = $(data).find("a"); 
 
    // for each item in links... 
 
    links.each(function(l) { 
 
     if (l > 0) { 
 
     // extract the href attr 
 
     var href = $(this).attr("href"); 
 
     $s.append('<li><a class="file-download" rel="nofollow" onclick="' + getAuthenticated(href) + '" href="' + href + '" >' + 
 
      $(this).text() + '</a></li>'); 
 
     } 
 

 
    }); 
 

 
    $("#downloadBtn").on("click", function(e) { 
 
     e.preventDefault(); 
 
     $('.file-download').multiDownload({ 
 
     delay: 500 
 
     }); 
 
    }); 
 

 
    }); 
 

 

 

 
});
.shell { 
 
    width: 300px; 
 
} 
 
.shell ul li { 
 
    list-style: none; 
 
} 
 
.shell a { 
 
    color: #232323; 
 
    display: block; 
 
    width: 100%; 
 
    height: 30px; 
 
    margin: .5em 0; 
 
    padding: 5px 10px; 
 
    font-weight: bold; 
 
    border: 1px solid #999; 
 
    border-radius: 3px; 
 
    text-decoration: none; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="shell"> 
 
    <!-- This is the place we're going to build our list --> 
 
    <ul> 
 
    </ul> 
 
    <input type="button" id="downloadBtn" value="Download ALL"> 
 
</div>

1

的身份驗證您使用的? 如果asp.net表單驗證,那麼你應該使用FormsAuthentication.SetAuthCookie,以節省您發送

,如果你的網站是提供給每一個和唯一的「文件」文件夾中是安全的憑據,然後 把另一個網頁在的.config「文件」文件夾,並配置

請參閱本文的基本認證 http://www.asp.net/web-api/overview/security/basic-authentication

我假設你使用Windows憑據工作,基本上就沒有這些憑證發送到AJAX請求的服務器,服務器應該在用戶進入你的網站後立即存儲它們。 因此,請確保您有Windows模式

<system.web> 
    <authentication mode="Windows" /> 
</system.web> 
+0

我使用基本身份驗證。 –