WCF REstful服务在Uri上传递用户和密码

时间:2016-08-17 14:45:54

标签: c# json rest wcf

我有一个WCF RESTful .NET 4.0服务,它运行并从URI的主体中消耗值#Json。

我调用方法的方式如下:

使用Poster,作为POST请求 http://localhost:25512/JSONService1.svc/CreateItem

体:

= YOLO

工作正常!

但我也想消耗uri,

http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111

这是我的代码

这是在Service1.cs上

[OperationContract]
[WebInvoke(UriTemplate = "CreateItem",  // ISSUES <<< not sure what to do here
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json, Method = "POST")]    

void CreateItem(Stream streamOfData,string uid, string pass);

以及与之相关的代码,Service1.svc.cs

public void CreateItem(Stream streamOfData, string uid, string pass)
{
        StreamReader reader = new StreamReader(streamOfData);
        String res = reader.ReadToEnd();
        NameValueCollection coll = HttpUtility.ParseQueryString(res);
}

1 个答案:

答案 0 :(得分:0)

要获取原始URI,您可以执行以下操作:

$UserName=Read-host "Please Enter Username: "

$ComputerName= @("computer1","computer2")
$profilefound = "false"

foreach($Computer in $ComputerName) {            
 Write-Verbose "Working on $Computer"            
 if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {            
  $Profiles = Get-WmiObject -Class Win32_UserProfile -Computer $Computer -ea 0            

  foreach($userprofile in $profiles){            
   $objSID = New-Object System.Security.Principal.SecurityIdentifier($userprofile.sid)            
   $objuser = $objsid.Translate([System.Security.Principal.NTAccount])            
   $profilename = $objuser.value.split("\")[1]          

   if($profilename -eq $UserName) {            
    $profilefound = "true"        

    try {            
     $userprofile.delete()            
     Write-Host -ForegroundColor Green "$UserName profile deleted successfully on $Computer"            
    } catch {            
     Write-Host -ForegroundColor Yellow "Failed to delete the profile, $UserName logged on to $Computer"            
    }            
   }           
  }

 } 
 else {            
  write-verbose "$Computer Not reachable"            
 } 
 if ($profilefound -eq "false") {            
   Write-Host -ForegroundColor Cyan "No profiles found on $Computer with Name $UserName"            
  }           
}

您可以使用System.Uri类和/或HttpUtility类来解析Uri。

使用System.Uri课程只需传入Uri。

string originalUri = System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString;

或在您的要求中:

Uri myUri = new Uri("http://localhost:25512/JSONService1.svc/CreateItem?uid=aaa&pass=111");

然后,您可以使用一些内置属性来检查Uri的各个部分。

相关问题