相关疑难解决方法(0)

我应该使用什么编码进行HTTP基本身份验证?

RFC2617表示将用户名和密码编码为base64,但没有说明在创建用于输入base64算法的八位字节时要使用的字符编码.

我应该假设US-ASCII或UTF8吗?或者有人已经在某处解决了这个问题?

http basic-authentication

73
推荐指数
3
解决办法
4万
查看次数

powershell http发布REST API基本身份验证

我使用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)

rest powershell

20
推荐指数
2
解决办法
4万
查看次数

如何强制WebRequest在POST期间发送授权标头

使用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字符串.

谢谢

.net twitter http webrequest

17
推荐指数
3
解决办法
7万
查看次数

Paypal REST api调用来自cURL但不来自C#代码

我试图从我的代码中调用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)

c# paypal

8
推荐指数
2
解决办法
4971
查看次数

如何保护 ASP.NET Web API 2

我需要实现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# asp.net asp.net-mvc asp.net-web-api asp.net-identity

4
推荐指数
1
解决办法
2182
查看次数