C#MVC API访问PHP通信(从C#MVC控制器获取/发送数据到PHP)

任何人都可以告诉我我有MVC 5(C#)API控制器,我希望使用PHP应用程序访问API数据 用户认证。 (没有用户身份验证我的PHP代码将工作)。 我已经创建了用户模型并为C#MVC Controller添加了顶部'[Authorize(Roles =“User”)]'。 我已经测试过这个用户名&amp; 密码它正在工作。</ p>

当我用我的PHP代码添加这些细节时,它对我来说不起作用。 它说“{”消息“:”此请求已被拒绝授权。“}”你能告诉我,为什么不工作,如果不是我的解决方案,请告诉我最佳解决方案。</ p>

C#MVC控制器</ strong> </ p>

  [Authorize(Roles =“User”)] 
公共类HolidayController:ApiController
{
return “值”;

}
</代码> </ PRE>

<强> HTML </强> </ p>

 <代码>&LT;  form method =“post”action =“index.php”enctype =“multipart / form-data”&gt; 
输入您的邮政编码:
&lt; input type =“text”name =“search”id =“txtSearch”value =“”&gt;
&lt;输入名称=“提交”类型=“提交”值=“搜索”/&gt;&lt; br /&gt;
&lt; / form&gt;
</ code> </ pre> \ n

PHP代码</ strong>

</ p>

  if(isset($ _ POST ['submit']))
{
/ /检查消息中的空值。

$ value = $ _POST ['search'];

$ ch = curl_init();

ncurl_setopt($ ch,CURLOPT_URL,'http:// www.myname.com/api/Holiday?pcode='.$value.'');
curl_setopt($ch,CURLOPT_POST,1);//将发布数据设置为true
curl_setopt($ ch,CURLOPT_POSTFIELDS,'Email = name&amp; Password = 123456'); // post data
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
$ json = curl_exec($ ch);

$ result = curl_exec($ ch);

print_r($ result);

$ Locations = json_decode($ json);

ncl_close($ ch);

foreach($ Locations as $ location)

{echo $ location-&gt; SubName。 }

}
?&gt;
</ code> </ pre>

它将打印 {“Message”:“此请求已拒绝授权。”} </ code> </ p>
</ div>

展开原文

原文

Can anyone please tell me I have MVC 5(C#) API controller, I want access API data using PHP application with user authentication. (without user authentication my PHP code will work). I have already created user model and add top for the C# MVC Controller '[Authorize(Roles = "User")]' . I have already tested this user name & password it is working.

When I add these details with my PHP code, it did not work for me. It say "{"Message":"Authorization has been denied for this request."}" Could you please tell me, why was not working, also If not good my solution, please tell me best solution.

C# MVC Controller

[Authorize(Roles = "User")]
public class HolidayController : ApiController
{
return "value";

}

HTML

<form method="post" action="index.php"  enctype="multipart/form-data">
Enter your Postcode: 
 <input type="text" name="search" id="txtSearch" value=""   >
<input name="submit" type="submit" value="Search"/><br />
</form>

PHP Code

if(isset($_POST['submit']))
{
// Checking null values in message.

$value = $_POST['search'];


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://www.myname.com/api/Holiday?pcode='.$value.'');
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS,'Email=name&Password=123456');   // post data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);

$result = curl_exec($ch);

print_r($result);

$Locations = json_decode($json); 


curl_close ($ch);

foreach($Locations as $location)

    { echo $location->SubName. }

}
?>

It will print {"Message":"Authorization has been denied for this request."}

3个回答



如果您想要一个简单的解决方案,并且您没有安全问题,可以使用 [AllowAnonymous] </ code >而不是 [Authorize(Roles =“User”)] </ code>如果你要返回一个JSon,你必须添加allowget选项</ p>

  public class HolidayController  :ApiController 
{
[AllowAnonymous]
public ActionResult YourMethod()
{
返回Json(data,JsonRequestBehavior.AllowGet);
}
}
</ code> </ pre>

希望这有帮助</ p>
</ div>

展开原文

原文

If you want a simple solution and you don't have problem with security, you could use [AllowAnonymous] instead of [Authorize(Roles = "User")] also if you are returning a JSon you must add the allowget option

public class HolidayController : ApiController
{
    [AllowAnonymous]    
    public ActionResult YourMethod()
    {
        return Json(data, JsonRequestBehavior.AllowGet);
    }
}

Hope this helps



使用标记 [Authorize(Roles =“User”)] </ code>只允许验证的Windows用户 打到那个网址。 您应该考虑继承 AuthorizeAttribute </ code>类。 以下是一个实现的一个很好的例子 http://www.diaryofaninja.com/blog/2011/07/24/writing-your-own-custom-aspnet-mvc-authorize-attributes 。 您将要更改 AuthorizeCore </ code>函数以检查验证您的电子邮件和密码字段,而不是使用 HttpContextBase.User </ code>来验证请求。</ p>
< / DIV>

展开原文

原文

Using the tag [Authorize(Roles = "User")] is only going to allow validated windows user to to hit that url. You should look into inheriting the AuthorizeAttribute class. Here is a good example of one implementation http://www.diaryofaninja.com/blog/2011/07/24/writing-your-own-custom-aspnet-mvc-authorize-attributes. You are going to want to change the AuthorizeCore function to check validate your email and password fields instead of using HttpContextBase.User to validate the request.



我认为您使用的是RestFul服务,如果确实需要检查是否允许 HTTP Basic </ strong> ,按照以下步骤操作:</ p>


  1. 安装一些RestFul测试器应用程序,例如Firefox的Rest Easy插件。</ li>
  2. 将URL插入, 将方法维护为GET(不要单击发送!)</ li>
  3. 在此网址的身份验证选项添加有效的用户和密码</ strong>。</ li>
  4. 点击发送按钮并检查结果。</ li>
    </ ol>

    因此,如果它有效,您需要在请求标题中添加HTTP基本身份验证信息。< / p>

    事情如下:curl_setopt($ process,CURLOPT_USERPWD,$ username。“:”。$ password); </ p>

    请参阅:如何使用HTTP基本身份验证发出请求 PHP curl? </ p>
    </ div>

展开原文

原文

I think you are using a RestFul service, if it's true you need to check if HTTP Basic is allowed, following this steps:

  1. Install some RestFul tester app such as the Rest Easy plugin for Firefox.
  2. Insert your URL in the, maintain the method as GET (Don't click send yet!)
  3. In the authentication options add a valid User and Password for this URL.
  4. Click on send button and check the result.

So, if it works you'll need add a HTTP basic authentication information in your request header.

Thing as: curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);

Please see: How do I make a request using HTTP basic authentication with PHP curl?

dongzhi6382
dongzhi6382 您可以使用代码示例按钮突出显示代码,或者使用单引号()来突出显示代码。
大约 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐