我很难在Web API上使用Facebook进行外部身份验证。这是我的控制器来处理Facebook按钮点击。
[Route("facebooklogin", Name= "FacebookLogin")]
[ValidateAntiForgeryToken()]
public async Task<ActionResult> FacebookLogin()
{
string facebookLink = string.Empty;
//Get available logins from api
using(HttpClient client = new HttpClient() { BaseAddress = new Uri(Request.Url.GetLeftPart(UriPartial.Authority)) })
{
var returnUrl = Url.RouteUrl("FacebookCallback");
var url = "/api/Account/ExternalLogins?returnUrl=" + HttpUtility.UrlEncode(returnUrl + "&generateState=true");
var response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
var loginList = await response.Content.ReadAsAsync<List<ExternalLoginViewModel>>();
facebookLink = loginList.Where(x => x.Name == "Facebook").Select(x => x.Url).FirstOrDefault();
//Send a get request to the facebook url
var facebookUrlResponse = await client.GetAsync(facebookLink);
facebookUrlResponse.EnsureSuccessStatusCode();
}
else { }
}
return View();
}
[Route("facebookcallback", Name="FacebookCallback")]
public ActionResult FacebookLoginCallback()
{
//Parse the access_token from facebook from the url
return View();
}
当我调试var facebookUrlResponse = await client.GetAsync(facebookLink);
时,我意识到我的请求不好。我的代码获取所有可用的外部登录提供程序,然后尝试使用为Facebook生成的可用URL来命中帐户控制器中的/ api / Account / ExternalLogin端点。我无法击中该端点。
我收到此错误消息:
响应状态代码不表示成功:400(错误请求)。
我打算让重定向uri进入FacebookCallback路线。然后我可以从uri解析access_token并进行进一步的游行。有人可以帮帮我吗?
我可以从邮递员那里收集这些信息:
在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.Net.Http.HttpRequestException:响应状态代码不表示成功:400(错误请求)。
源错误是:
//向facebook网址发送获取请求 第135行:var facebookUrlResponse = await client.GetAsync(facebookLink);
这也是堆栈跟踪:
[HttpRequestException:响应状态代码不表示成功:400(错误请求)。] System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()+92180 Blinkas.Web.Controllers。&lt; FacebookLogin&gt; d__6.MoveNext()在C:\ Users \ Williams \ Documents \ Visual Studio 2015 \ Projects \ Blinkas \ Blinkas.Web \ Controllers \ HomeController.cs:136 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)+92 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)+58 System.Runtime.CompilerServices.TaskAwaiter.GetResult()+26 System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(任务任务)+42 System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)+73 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass37。&lt; BeginInvokeAsynchronousActionMethod&gt; b__36(IAsyncResult asyncResult)+37 System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+68 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+34 System.Web.Mvc.Async.AsyncInvocationWithFilters。&lt; InvokeActionMethodFilterAsynchronouslyRecursive&gt; b__3d()+69 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass46。&lt; InvokeActionMethodFilterAsynchronouslyRecursive&gt; b__3f()+ 230 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass33。&lt; BeginInvokeActionMethodWithFilters&gt; b__32(IAsyncResult asyncResult)+27 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+68 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass2b。&lt; BeginInvokeAction&gt; b__1c()+42 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21。&lt; BeginInvokeAction&gt; b__1e(IAsyncResult asyncResult)+124 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +27 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+30 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+29 System.Web.Mvc.Controller。&lt; BeginExecuteCore&gt; b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState)+27 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+21 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+32 System.Web.Mvc.Controller。&lt; BeginExecute&gt; b__15(IAsyncResult asyncResult,Controller controller)+26 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +40 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+21 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)+29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)+24 System.Web.Mvc.MvcHandler。&lt; BeginProcessRequest&gt; b__5(IAsyncResult asyncResult,ProcessRequestState innerState)+27 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+30 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult,Object tag)+21 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)+29 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)+23 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar)+129