C# api项目上的Identity server身份验证
我有两个项目。 测试客户端mvc和web api(identity server是第三个项目)。 我想在api上进行身份验证。 我花了这么多时间在这上面,但还是没有成功 客户:C# api项目上的Identity server身份验证,c#,web-services,visual-studio-2015,asp.net-core-mvc,C#,Web Services,Visual Studio 2015,Asp.net Core Mvc,我有两个项目。 测试客户端mvc和web api(identity server是第三个项目)。 我想在api上进行身份验证。 我花了这么多时间在这上面,但还是没有成功 客户: public async Task<ActionResult> CallBackend() { var client = HttpClientHelper.GetClient(null, User.Identity); //api call is not suc
public async Task<ActionResult> CallBackend()
{
var client = HttpClientHelper.GetClient(null, User.Identity);
//api call is not succes.
//Variable egsResponse have value : {StatusCode: 500, ReasonPhrase: 'Internal Server Error',
//Version: 1.1, Content: System.Net.Http.StreamContent, Headers:... –
HttpResponseMessage egsResponse = await client.GetAsync("api/secured");
IEnumerable<Claim> result = null;
if (egsResponse.IsSuccessStatusCode)
{
string egsContent = await egsResponse.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<IEnumerable<Claim>>(egsContent);
}
return View(result);
}
public异步任务CallBackend()
{
var client=HttpClientHelper.GetClient(null,User.Identity);
//api调用不成功。
//变量egsResponse的值:{StatusCode:500,原因短语:'Internal Server Error',
//版本:1.1,内容:System.Net.Http.StreamContent,标题:…-
HttpResponseMessage egsResponse=wait client.GetAsync(“api/secured”);
IEnumerable结果=null;
if(egsResponse.IsSuccessStatusCode)
{
字符串egsContent=await egsResponse.Content.ReadAsStringAsync();
结果=JsonConvert.DeserializeObject(egsContent);
}
返回视图(结果);
}
api:
[HttpGet]
[授权]
public IActionResult Get()
{
var identity=this.User.identity作为ClaimsIdentity;
IEnumerable索赔=null;
索赔=用户索赔;
返回Ok(新的ObjectResult(索赔));
}
启动api
public class Startup
{
public Startup(IHostingEnvironment env)
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
var factory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
factory.AddConsole();
//error is probably here !!!!!!!
//something missing to make identoty server visible for api project.
app.UseJwtBearerAuthentication(options =>
{
options.Authority = "http://localhost:54540/";
options.Audience = "myClient";
});
app.UseMvc();
}
}
公共类启动
{
公共启动(IHostingEnvironment环境)
{
}
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
}
//在调用ConfigureServices之后调用Configure。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
//配置HTTP请求管道。
app.UseStaticFiles();
var factory=app.ApplicationServices.GetRequiredService();
factory.AddConsole();
//错误可能在这里!!!!!!!
//缺少使Identity server对api项目可见的内容。
app.UseJwtBearerAuthentication(选项=>
{
选项。权限=”http://localhost:54540/";
options.acquisition=“myClient”;
});
app.UseMvc();
}
}
我得到一个错误:
System.InvalidOperationException使用以下身份验证方案
未被接受:
在
Microsoft.AspNet.Http.Authentication.Internal.DefaultAuthenticationManager.d_u10.MoveNext()
在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上
在
Microsoft.AspNet.Mvc.ActionResults.ChallengeResult.d_u14.MoveNext()
在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上
在
Microsoft.AspNet.Mvc.Actions.FilterActionInvoker.d_u53.MoveNext()
在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上
在
Microsoft.AspNet.Mvc.Actions.FilterActionInvoker.d_u41.MoveNext()
在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上
在
Microsoft.AspNet.Mvc.Actions.MvcRouteHandler.d_u6.MoveNext()
在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上
在
Microsoft.AspNet.Mvc.Routing.InnerAttributeRoute.d_u10.MoveNext()
在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
任务)在
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上
在
Microsoft.AspNet.Routing.RouteCollection.d_u9.MoveNext()在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上 在Microsoft.AspNet.Builder.RouterMiddleware.d_u4.MoveNext()上 在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上 在 Microsoft.AspNet.Authentication.AuthenticationMiddleware.d_u18.MoveNext() ---在Microsoft.AspNet.Authentication.AuthenticationMiddleware.d_u18.MoveNext()中返回异常 在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上 在 Microsoft.AspNet.Hosting.Internal.RequestServicesContainerMiddleware.d_u3.MoveNext() 在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter.GetResult()上 在 Microsoft.AspNet.Hosting.Internal.hostingingine.c_udisplayClass29_0.d.MoveNext() 在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务 任务)在 System.Runtime.CompilerServices.TaskAw
public class Startup
{
public Startup(IHostingEnvironment env)
{
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
var factory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
factory.AddConsole();
//error is probably here !!!!!!!
//something missing to make identoty server visible for api project.
app.UseJwtBearerAuthentication(options =>
{
options.Authority = "http://localhost:54540/";
options.Audience = "myClient";
});
app.UseMvc();
}
}