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.

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?