我的Odata操作参数未解析/反序列化。
我正在使用dotnet core 2.2来显示OData控制器。
我需要采取无限制的行动。 OData路由引擎不会反序列化操作参数(UserDto userDto):
[AllowAnonymous]
[HttpPost]
[ODataRoute(Routing.Endpoints.UserRoutes.AUTHENTICATE)]
public async Task<IActionResult> Authenticate(UserDto userDto)
{
var user = await _userService.Authenticate(userDto?.Username, userDto?.Password);
if (user == null)
return BadRequest("Username or password is incorrect");
var dto = Mapper.Map<UserDto>(user);
return Ok(dto);
}
这是我的配置:
app.UseMvc(routeBuilder =>
{
var odataBuilder = new ODataConventionModelBuilder(app.ApplicationServices);
odataBuilder.EnableLowerCamelCase();
odataBuilder.EntitySet<BookDto>(nameof(Book));
odataBuilder.EntitySet<UserDto>(nameof(User));
var authenticate = odataBuilder.Action(Routing.Endpoints.UserRoutes.AUTHENTICATE);
authenticate.Parameter<UserDto>("userDto");
routeBuilder.Select().Expand().Filter().OrderBy().Count().MaxTop(int.MaxValue);
routeBuilder.MapODataServiceRoute("odata", string.Empty, odataBuilder.GetEdmModel());
});
这是UserDto:
public class UserDto
{
[Key] public Guid Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Token { get; set; }
}
我发帖时
该操作由路由引擎解决-但该参数没有“用户名”和“密码”值:
如果我在参数上使用[FromBody]
属性-“ userDto”参数为null:
该架构似乎正确:
<Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">
<Action Name="authenticate">
<Parameter Name="userDto" Type="ExampleApi.Dto.UserDto"/>
</Action>
<EntityContainer Name="Container">
<EntitySet Name="Book" EntityType="ExampleApi.Dto.BookDto"/>
<EntitySet Name="User" EntityType="ExampleApi.Dto.UserDto"/>
<ActionImport Name="authenticate" Action="Default.authenticate"/>
</EntityContainer>
</Schema>
我尝试了以下操作:Action Parameter Support
甚至是Microsoft的版本(尽管有日期):Actions and Functions in OData
整天都在敲我的头...
答案 0 :(得分:3)
您只能使用简单的WebApi属性来实现身份验证
public class UserController : ODataController
{
[AllowAnonymous]
[HttpPost("user/auth")]
public async Task<IActionResult> Authenticate([FromBody] UserDto userDto)
{
return Ok(userDto);
}
}