C# 管道请求有问题。系统刚刚退出并没有';不要继续
我有一个自定义中间件的方法。这完全是从上一个解决方案复制的,没有任何更改C# 管道请求有问题。系统刚刚退出并没有';不要继续,c#,.net-core,C#,.net Core,我有一个自定义中间件的方法。这完全是从上一个解决方案复制的,没有任何更改 using JobsLedger.AUTHORISATION.API.SessionMiddleware.Interfaces; using JobsLedger.INTERFACES; using Microsoft.AspNetCore.Http; using System; using System.Linq; using System.Threading.Tasks; namespace JobsLedger.A
using JobsLedger.AUTHORISATION.API.SessionMiddleware.Interfaces;
using JobsLedger.INTERFACES;
using Microsoft.AspNetCore.Http;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace JobsLedger.AUTHORISATION.API.SessionMiddleware
{
public class ConfigureSessionMiddleware
{
private readonly RequestDelegate _next;
public ConfigureSessionMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext, IUserSession userSession, ISessionServices sessionServices)
{
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
if (userSession == null)
{
throw new ArgumentNullException(nameof(userSession));
}
if (sessionServices == null)
{
throw new ArgumentNullException(nameof(sessionServices));
}
if (httpContext.User.Identities.Any(id => id.IsAuthenticated))
{
if (httpContext.Session.GetString("connectionString") == null) // Session needs to be set..
{
userSession.UserId = httpContext.User.Claims.FirstOrDefault(x => x.Type == "userId")?.Value;
userSession.ConnectionString = sessionServices.ConnectionStringFromUserId(userSession.UserId);
httpContext.Session.SetString("userId", userSession.UserId);
httpContext.Session.SetString("connectionString", userSession.ConnectionString);
}
else // Session set so all we need to is to build userSession for data access..
{
userSession.UserId = httpContext.Session.GetString("userId");
userSession.ConnectionString = httpContext.Session.GetString("connectionString");
}
}
// Call the next delegate/middleware in the pipeline
await _next.Invoke(httpContext).ConfigureAwait(false);
}
}
}
您会注意到底部或最后一行有await\u next.Invoke(httpContext.ConfigureAwait(false))代码>应该调用下一个中间件。事实并非如此。将我直接发送回浏览器上的本地主机。我怀疑它应该处理请求,但它只是跳出管道,我不知道
为什么它要把我甩出去
如何调试它
我能看看下一个项目吗?(即使它不会出现)
更新
我有一个以前的解决方案,我重构了当前的解决方案。我在3.1中运行了它,并检查了该请求,并将其与当前的解决方案httpcontext请求进行了比较。这里是-唯一的区别是新的或重构的解决方案有https(最上面的是跳出请求管道的当前解决方案)。这会有区别吗?会不会是找不到控制器
这是我要联系的控制器
using JobsLedger.API.ViewModels.Auth;
using JobsLedger.AUTHORISATION;
using JobsLedger.AUTHORISATION.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Security.Claims;
using System.Threading.Tasks;
namespace JobsLedger.API.Controllers.Authorisation {
[Route("api/[controller]")]
public class JwtController : Controller {
#region Variables
private readonly IUserAuthorisationServices _tokenService;
private readonly IOptions<JwtIssuerOptions> jwtOptions;
private readonly ILogger logger;
private readonly JsonSerializerSettings _serializerSettings;
#endregion
public JwtController(IUserAuthorisationServices tokenService,
IOptions<JwtIssuerOptions> jwtOptions,
ILoggerFactory loggerFactory) {
if (loggerFactory is null) throw new ArgumentNullException(nameof(loggerFactory));
_tokenService = tokenService ?? throw new ArgumentNullException(nameof(tokenService));
jwtOptions = jwtOptions ?? throw new ArgumentNullException(nameof(jwtOptions));
logger = loggerFactory.CreateLogger<JwtController>();
_serializerSettings = new JsonSerializerSettings {
Formatting = Formatting.Indented
};
//loggingRepository = _errorRepository;
//ThrowIfInvalidOptions(this.jwtOptions);
}
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Get([FromBody] LoginViewModel model)
{
if (model is null) throw new ArgumentNullException(nameof(model));
var userContext = _tokenService.ValidateUser(model.Email, model.Password);
if (userContext.Principal == null) {
logger.LogInformation($"Invalid username ({model.Email}) or password ({model.Password})");
return BadRequest("Invalid credentials");
}
使用JobsLedger.API.ViewModels.Auth;
使用批发商授权;
使用批发商、授权、接口;
使用Microsoft.AspNetCore.Authorization;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Extensions.Logging;
使用Microsoft.Extensions.Options;
使用Newtonsoft.Json;
使用制度;
使用System.Security.Claims;
使用System.Threading.Tasks;
命名空间JobsLedger.API.Controllers.authorization{
[路由(“api/[控制器]”)]
公共类JwtController:控制器{
#区域变量
专用只读IUSerAuthorizationServices\u令牌服务;
私有只读IOptions jwtOptions;
专用只读ILogger记录器;
私有只读JsonSerializerSettings _serializerSettings;
#端区
公共JwtController(IUSerAuthorizationServices令牌服务,
IOptions jwtOptions,
伊洛格工厂(伐木厂){
如果(loggerFactory为null)抛出新的ArgumentNullException(nameof(loggerFactory));
_tokenService=tokenService??抛出新的ArgumentNullException(nameof(tokenService));
jwtOptions=jwtOptions??抛出新的ArgumentNullException(nameof(jwtOptions));
logger=loggerFactory.CreateLogger();
_serializerSettings=新JsonSerializerSettings{
格式化=格式化。缩进
};
//loggingRepository=\u errorRepository;
//ThrowIfInvalidOptions(this.jwtOptions);
}
[HttpPost]
[异名]
公共异步任务Get([FromBody]LoginViewModel模型)
{
如果(model为null)抛出新ArgumentNullException(nameof(model));
var userContext=_tokenService.ValidateUser(model.Email,model.Password);
if(userContext.Principal==null){
logger.LogInformation($“无效的用户名({model.Email})或密码({model.password})”;
返回请求(“无效凭证”);
}
我感觉您在错误的位置注入了依赖项。您需要在构造函数中注入依赖项,然后在InvokeAsync上使用HttpContext。因此,当依赖项为null时,它可能会引发异常
private readonly RequestDelegate _next;
private readonly IUserSession _userSession;
private readonly ISessionServices _sessionServices;
public ConfigureSessionMiddleware(RequestDelegate next, IUserSession userSession, ISessionServices sessionServices)
{
_next = next;
_userSession = userSession;
_sessionServices = sessionServices;
}
public async Task InvokeAsync(HttpContext httpContext)
{
//rest of your code
}
如果您遇到500个错误,那么您可以在Startup.cs中的Configure开始处放置类似的内容,这将告诉您开发中的错误
app.UseExceptionHandler(options =>
{
options.Run(async context =>
{
var ex = context.Features.Get<IExceptionHandlerPathFeature>();
if (ex?.Error != null)
{
Debugger.Break();
}
});
});
app.UseExceptionHandler(选项=>
{
运行(异步上下文=>
{
var ex=context.Features.Get();
如果(ex?.Error!=null)
{
Debugger.Break();
}
});
});
感谢您的响应..不确定这是否正确..在初始传递中,它检查id是否未找到,并跳过connectionstring部分..问题是它命中下一条语句并跳出管道。我有一个相同的早期版本(以及启动中的配置方法-无更改)这将直接跳到JWT控制器。当前的一个跳出。不确定除了在调用中使用断点和try-catch之外还有什么建议。您还可以在ConfigureServices中重复检查您是否添加了与其他项目相同的内容。这很奇怪,因为ConfigureServices是相同的。我确实中断了,但它只是j直接转到浏览器-可能是因为它没有找到控制器而停止了吗?响应是:POST 500服务/auth/auth-service.AuthService.login@auth service.ts:52 public/components/login/login.login.submitLogin@login.ts:33 evaluate@aurelia binding.js:1517 callSource@aurelia binding.js:5232 handleEvent@aurelia binding.js:5241 handleDelegatedEvent@aurelia binding.js:3306身份验证服务。ts:59状态:500身份验证服务。ts:96捕获错误语法错误:JSON中位于0位置的意外标记看起来您遇到了内部服务器错误,因此我编辑了我的答案,您可以在开发时检查错误。