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.

    评论

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑