简体   繁体   English

如何在WCF Rest服务中通过不带URI的POST发送数据

[英]How to send data over POST without URI in WCF rest services

I am new to WCF. 我是WCF的新手。 I am trying to implement a restful service for authentication. 我正在尝试实现一种宁静的身份验证服务。

My WCF Code is as below. 我的WCF代码如下。

    [OperationContract]
    [WebInvoke(Method="POST", UriTemplate = "auth", RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped)]
    Employee authEmployee(String username, String password);

What I need to do here is get username and password without receiving it via URL. 我需要在这里获取用户名和密码,而无需通过URL接收用户名和密码。 How do I achieve this? 我该如何实现?

This might be a basic question for experts, but I am new to this. 对于专家来说,这可能是一个基本问题,但是我对此并不陌生。 Any help is appreciated. 任何帮助表示赞赏。

Thanks. 谢谢。

I'll rewrite my comments here for clarity. 为了清楚起见,我将在这里重写我的评论。

Your client side should call service method by creating request and setting properly request fields, for example 您的客户端应通过创建请求并正确设置请求字段来调用服务方法,例如

HttpWebRequest req =(HttpWebRequest)HttpWebRequest.Create(uri);

req.Method = "Post"; 
req.ContentType = "application/json";
byte[] bodyBytes = Encoding.UTF8.GetBytes("{\"Name\":\"John Doe\",\"Age\":33}");

req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);  
req.GetRequestStream().Close();

and of course 而且当然

resp = (HttpWebResponse)req.GetResponse();

in that way consumer has to pass values but that values do not travel directly through url 这样,消费者必须传递值,但该值不能直接通过url传递

I think that method name is passed in URI and only attributes that this method needs are passed inside json. 我认为方法名称是通过URI传递的,并且仅此方法需要的属性在json中传递。

{ "action": "Authenticate", "attributes": { "AccountName": "admin", "Password": "password" } }. {“操作”:“验证”,“属性”:{“帐户名”:“管理员”,“密码”:“密码”}}。 This is how client is sending data. 这就是客户端发送数据的方式。 So should I have 3 arguments in my service method? 那么我的服务方法中应该有3个参数吗? for Action, AccountName and Password? 用于操作,帐户名和密码?

In this case your service operation should look like this: 在这种情况下,您的服务操作应如下所示:

Employee authEmployee(string action, attributes attr);

Define the attributes type as: 将属性类型定义为:

class attributes
{
    string AccountName { get; set; }
    string Password { get; set; }
} 

As a side note, it's not particularly secure if you're passing credentials in as plaintext in this manner. 附带说明一下,如果您以这种方式以纯文本形式传递凭据,则不是特别安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM