RFC2617表示将用户名和密码编码为base64,但没有说明在创建用于输入base64算法的八位字节时要使用的字符编码.
我应该假设US-ASCII或UTF8吗?或者有人已经在某处解决了这个问题?
我使用curl使用REST API进行基本认证:
curl -X POST -H 'Accept: application/json' -u user:password http://localhost/test/
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用powershell webRequest时,我得到403(许可被拒绝).当我在REST代码中禁用身份验证检查时,此脚本正常工作.
powershell在POST请求上传递凭据的最佳方式是什么,类似于curl,或者我可以做些什么来修复以下脚本.
非常感谢对此的一些指导.谢谢.
这是我的powershell脚本:
function Execute-HTTPPostCommand() {
param(
[string] $target = $null
)
$username = "user"
$password = "pass"
$webRequest = [System.Net.WebRequest]::Create($target)
$webRequest.ContentType = "text/html"
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post)
$webrequest.ContentLength = $PostStr.Length
$webRequest.ServicePoint.Expect100Continue = $false
$webRequest.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password
$webRequest.PreAuthenticate = $true
$webRequest.Method = "POST"
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($PostStr, 0,$PostStr.length)
v$requestStream.Close()
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] …Run Code Online (Sandbox Code Playgroud) 使用WebRequest发送POST时,即使我手动设置了标头并将PreAuthenticate设置为true,也不会随请求一起发送Authorization标头,例如:
webRequest.Headers["Authorization"] = "OAuth oauth_consumer_key=bFPD...";
webRequest.PreAuthenticate = true;
Run Code Online (Sandbox Code Playgroud)
使用Fiddler我可以看到没有发送Authorization标头.目标站点(Twitter)返回400(错误请求)而不是401(未授权),因此WebRequest发送授权数据所需的错误挑战.有关信息,返回的内容是:
{"errors":[{"message":"Bad Authentication data","code":215}]}
Run Code Online (Sandbox Code Playgroud)
那么,我该如何解决这个问题呢?如何强制WebRequest在初始请求时发送授权?请注意,授权数据不是基本身份验证,而是OAuth字符串.
谢谢
我试图从我的代码中调用Paypal api.我设置了沙盒帐户,当我使用curl但它的代码工作方式不同时,它会起作用,返回401 Unauthorized.
这是Paypal记录的curl命令
curl https://api.sandbox.paypal.com/v1/oauth2/token -H "Accept: application/json" -H "Accept-Language: en_US" -u "A****:E****" -d "grant_type=client_credentials"
Run Code Online (Sandbox Code Playgroud)
更新:显然.Credentials不会做的伎俩,而是Authorization手动设置标题工作(见代码)
这是代码(修剪到它的本质):
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token");
request.Method = "POST";
request.Accept = "application/json";
request.Headers.Add("Accept-Language:en_US")
// this doesn't work:
**request.Credentials = new NetworkCredential("A****", "E****");**
// DO THIS INSTEAD
**string authInfo = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("A****:E****"));**
**request.Headers["Authorization"] = "Basic " + authInfo;**
using (StreamWriter swt = new StreamWriter(request.GetRequestStream()))
{
swt.Write("grant_type=client_credentials");
}
request.BeginGetResponse((r) =>
{
try
{
HttpWebResponse response = request.EndGetResponse(r) as HttpWebResponse; // …Run Code Online (Sandbox Code Playgroud) 我需要实现ASP.NET Web API 2即
[RoutePrefix("orders")]
public class OrdersController : ApiController
{
[Route("{id}")]
public Order Get(int id) { }
[Route("{id}/approve")]
public Order Approve(int id) { }
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何保护它?
例如,我们可以为此使用ASP.NET Identity吗?
有什么线索吗?
c# ×2
http ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
paypal ×1
powershell ×1
rest ×1
twitter ×1
webrequest ×1