FluentValidation 访问 IOwinContext - 获取请求 IP 并访问 ApplicationDbContext
FluentValidation access IOwinContext - get request IP and access ApplicationDbContext
我创建了 CreateAccountValidator
class,它负责在 IIS 上托管的 WebAPI2 应用程序中验证我的绑定模型。
下面是我的class:
public class CreateAccountValidator : AbstractValidator<CreateAccountBindingModel>
{
public CreateAccountValidator()
{
RuleFor(u => u.Amount)
.Cascade(CascadeMode.StopOnFirstFailure)
.GreaterThan(0).WithMessage("Must be greater than 0");
RuleFor(u => u.FirstName)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("Name is required")
.Length(3, 20).WithMessage("Name must be between 3 and 20 characters");
RuleFor(u => u.LastName)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("Surname is required")
.Length(3, 20).WithMessage("Surname must be between 3 and 20 characters");
RuleFor(u => u.ID)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("ID is required")
.Must(ValidateId).WithMessage("ID is invalid");
}
private bool ValidateId(CreateAccountBindingModel createAccountBindingModel, string id, PropertyValidatorContext context)
{
var id_valid = IdValidator.IsValid(id);
if (!id_valid)
{
using (var db = new ApplicationDbContext())
{
//get request IP!!!
db.SaveAlert(createAccountBindingModel.UserEmail, "ID - CHECKSUM", string.Format("User entered: {0}", id), "192.100.100.100");
return false;
}
}
return true;
}
}
在 ValidateId
方法中我正在调用我的自定义验证器,如果它 returns false 我想将该事实记录到数据库中。
我需要获取请求 IP,但我不知道该怎么做。我没有 IOwinContext 或 Request 属性。在我调用的 Api 个控制器中:
Request.GetOwinContext().Get<ApplicationDbContext>()
我可以从我的应用程序中的 classes 访问 IOwinContext 吗?是,那我该怎么做?
所以 HttpContext
在 ASP.NET 中是静态的。
所以HttpContext.Current.Request.GetOwinContext()
但是,您在问题中正确使用了 ApplicationDbContext
,我不会更改它
原因是,您只想围绕尽可能小的工作单元实例化新上下文。你做的完全正确。
我创建了 CreateAccountValidator
class,它负责在 IIS 上托管的 WebAPI2 应用程序中验证我的绑定模型。
下面是我的class:
public class CreateAccountValidator : AbstractValidator<CreateAccountBindingModel>
{
public CreateAccountValidator()
{
RuleFor(u => u.Amount)
.Cascade(CascadeMode.StopOnFirstFailure)
.GreaterThan(0).WithMessage("Must be greater than 0");
RuleFor(u => u.FirstName)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("Name is required")
.Length(3, 20).WithMessage("Name must be between 3 and 20 characters");
RuleFor(u => u.LastName)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("Surname is required")
.Length(3, 20).WithMessage("Surname must be between 3 and 20 characters");
RuleFor(u => u.ID)
.Cascade(CascadeMode.StopOnFirstFailure)
.NotEmpty().WithMessage("ID is required")
.Must(ValidateId).WithMessage("ID is invalid");
}
private bool ValidateId(CreateAccountBindingModel createAccountBindingModel, string id, PropertyValidatorContext context)
{
var id_valid = IdValidator.IsValid(id);
if (!id_valid)
{
using (var db = new ApplicationDbContext())
{
//get request IP!!!
db.SaveAlert(createAccountBindingModel.UserEmail, "ID - CHECKSUM", string.Format("User entered: {0}", id), "192.100.100.100");
return false;
}
}
return true;
}
}
在 ValidateId
方法中我正在调用我的自定义验证器,如果它 returns false 我想将该事实记录到数据库中。
我需要获取请求 IP,但我不知道该怎么做。我没有 IOwinContext 或 Request 属性。在我调用的 Api 个控制器中:
Request.GetOwinContext().Get<ApplicationDbContext>()
我可以从我的应用程序中的 classes 访问 IOwinContext 吗?是,那我该怎么做?
所以 HttpContext
在 ASP.NET 中是静态的。
所以HttpContext.Current.Request.GetOwinContext()
但是,您在问题中正确使用了 ApplicationDbContext
,我不会更改它
原因是,您只想围绕尽可能小的工作单元实例化新上下文。你做的完全正确。