Asp.net 使用facebook外部提供商登录Web API
我很难在Web API上使用Facebook进行外部身份验证。这是我处理Facebook按钮点击的控制器Asp.net 使用facebook外部提供商登录Web API,asp.net,facebook,oauth-2.0,asp.net-web-api2,asp.net-identity,Asp.net,Facebook,Oauth 2.0,Asp.net Web Api2,Asp.net Identity,我很难在Web API上使用Facebook进行外部身份验证。这是我处理Facebook按钮点击的控制器 [Route("facebooklogin", Name= "FacebookLogin")] [ValidateAntiForgeryToken()] public async Task<ActionResult> FacebookLogin() { string facebookLink = string.Empty;
[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();
}
[路由(“facebooklogin”,Name=“facebooklogin”)]
[ValidateAntiForgeryToken()]
公共异步任务FacebookLogin()
{
string facebookLink=string.Empty;
//从api获取可用的登录名
使用(HttpClient client=newhttpclient(){BaseAddress=newURI(Request.Url.GetLeftPart(UriPartial.Authority))})
{
var returnUrl=Url.RouteUrl(“FacebookCallback”);
var url=“/api/Account/ExternalLogins?returnUrl=“+HttpUtility.UrlEncode(returnUrl+”&generateState=true”);
var response=wait client.GetAsync(url);
response.EnsureSuccessStatusCode();
if(响应。IsSuccessStatusCode)
{
var loginList=wait response.Content.ReadAsAsync();
facebookLink=loginList.Where(x=>x.Name==“Facebook”)。选择(x=>x.Url)。FirstOrDefault();
//向facebook url发送get请求
var facebookUrlResponse=await client.GetAsync(facebookLink);
facebookUrlResponse.EnsureAccessStatusCode();
}
else{}
}
返回视图();
}
[路由(“facebookcallback”,Name=“facebookcallback”)]
公共操作结果FacebookLoginCallback()
{
//从url解析facebook的访问令牌
返回视图();
}
当我调试到var facebookUrlResponse=wait client.GetAsync(facebookLink)时代码>,我意识到我有一个错误的请求。我的代码获取所有可用的外部登录提供程序,然后尝试使用为Facebook生成的url来点击帐户控制器中的/api/Account/ExternalLogin端点。我无法到达终点
我收到以下错误消息:
响应状态代码不表示成功:400(错误请求)
我计划将重定向uri发送到FacebookCallback路由。然后,我可以从uri解析访问令牌并进行进一步处理。谁能帮帮我吗
我可以从邮递员那里收集到以下信息:
在执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.Net.Http.HttpRequestException:响应状态代码不表示成功:400(错误请求)
源错误是:
//向facebook url发送get请求
第135行:var facebookUrlResponse=await client.GetAsync(facebookLink)
这也是stacktrace:
[HttpRequestException:响应状态代码不表示成功:400(错误请求)。]
System.Net.Http.HttpResponseMessage.EnsureAccessStatusCode()+92180
C:\Users\Williams\Documents\Visual Studio 2015\Projects\Blinkas\Blinkas.Web\Controllers\HomeController.cs:136中的Blinkas.Web.Controllers.d\uu 6.MoveNext()
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+92
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+58
System.Runtime.CompilerServices.TaskWaiter.GetResult()+26
System.Threading.Tasks.TaskHelperExtensions.ThrowIfFaulted(任务)+42
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)+73
System.Web.Mvc.Async.c__显示类37.b__36(IAsyncResult asyncResult)+37
System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)+27
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58
System.Web.Mvc.Async.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+68
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)+34
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d()+69
System.Web.Mvc.Async.c__显示类46.b_3f()+230
System.Web.Mvc.Async.c__显示类33.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.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+68
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34
System.Web.Mvc.Async.c__显示类2B.b__1c()+42
System.Web.Mvc.Async.c__显示类21.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.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+30
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)+29
System.Web.Mvc.Controller.b_u1d(IAsyncResult asyncResult,ExecuteCorerate innerState)+27
System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult)+48
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End()+58
System.Web.Mvc.Async.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+30
System.Web.Mvc.Async.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+21
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+32
System.Web.Mvc.Controller.b_uu15(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.asyncResultRapper.End(IAsyncResult asyncResult,对象标记)+30
System.Web.Mvc.Async.AsyncResultRapper.End(IAsyncResult asyncResult,对象t