我目前无法访问我的api控制器操作。我有一个MVC应用程序,通过我的javascript中的ajax调用来调用api。
我的API方法是:
[HttpPost, Route("api/Communication/AddLink")]
public HttpResponseMessage AddLink([FromBody]int id)
{
//DO STUFF
return Request.CreateResponse(HttpStatusCode.OK, returnData);
}
我的ajax电话是:
$.ajax({
cache: false,
url: serviceEndpoint + 'api/Communication/AddLink',
type: 'POST',
data: { id: commid },
headers:{
Authorization: 'Bearer ' + token
},
success: function (data) {
console.log(data);
}
});
我按如下方式检索我的令牌:
public async static Task<AuthenticationResult> GetTokenAsync(AuthenticationContext ctx, string resourceId)
{
ClientCredential credential = new ClientCredential(OfficeSettings.ClientId, OfficeSettings.ClientSecret);
var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
UserIdentifier ident = new UserIdentifier(userObjectId, UserIdentifierType.UniqueId);
var redirectUrl = new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path));
try
{
AuthenticationResult result = await ctx.AcquireTokenSilentAsync(resourceId, credential, ident);
//var result = await ctx.AcquireTokenAsync(resourceId, credential);
LastAuthority = ctx.Authority;
return result;
}
catch (AdalException e)
{
logger.Error(e, "AcquireTokenSilentAsync: " + e.Message);
ctx.TokenCache.Clear();
return null;
}
catch (Exception ex)
{
throw ex;
}
}
这是我的WebApiConfig.cs路由:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.EnableCors();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// DI Config
var container = new UnityContainer();
/// Register Types
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
}
如果我将[AllowAnonymous]
属性添加到我的API操作但在应用程序内部无效,则这适用于fiddler但是由于显而易见的原因,这不是解决方案。
当我通过提琴手尝试时,我得到错误:
IDX10803: Unable to create to obtain configuration from: 'https://login.microsoftonline.com/.well-known/openid-configuration'
任何有关这方面的帮助都将非常感激,因为我对这一点的想法不合适。如果需要,我很乐意提供更多代码和信息。
谢谢,
修改
一些额外的信息。我的Api设置为从localhost:44391开始,但是当它加载时,url是https://localhost:44391/Error/GenericError。这可能表明启动时出现了问题吗?如果是这样,我该如何正确调试?