我的项目包含一个ASP.NET MVC应用程序(不进行身份验证)和两个Web API。
第一个API(帐户API)使用ASP.NET身份在Owin中间件之上构建,以便对ASP.NET MVC进行基于令牌的身份验证。
这对于ASP.NET MVC非常有效。
但是第二个API是数据处理程序API(数据API)。
问题是我需要授权Data API上的传入请求。在ASP.NET MVC应用程序中,我有以下请求。
开始编辑1
// The MVC controller makes a request in order to authenticate the user.
// If the authentication succeeds, an access token is returned.
var authorizationUrl = "my.authorization.api";
HttpResponseMessage result = httpClient.PostAsync(authorizationUrl, content).Result;
// In the Home controller I have these lines of code.
var user = ((ClaimsPrincipal) HttpContext.Current.User);
var token = x.Claims.First(t => t.Type == "AccessToken").Value;
// Now I just need to call the Data API.
var dataUrl = "my.data.api/home";
var request = WebRequest.Create(dataUrl) as HttpWebRequest;
request.Method = WebRequestMethods.Http.Get;
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + accessToken);
var response = (HttpWebResponse) request.GetResponse();
数据API在HomeController上具有[Authorize]属性,每当我调用任何方法时,我都会收到401未经授权。
结束编辑1
问题是:如何使用请求标头(从MVC应用程序发送)中的accessToken验证传入请求(在Data API上)?
谢谢!