dsa111111 2015-07-17 05:06
浏览 92

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

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条回答

  • dtl85148 2016-05-26 02:05
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)