我是使用OpenIddict Rc3的人。我对密码流中的代码进行了一些小的更改,然后尝试运行它。但是SignIn
方法返回500。我尝试了很多搜索,但没有找到解决方案。
这是我的代码:
StartUp.cs
services.AddOpenIddict().AddCore(options =>
{
options.UseEntityFrameworkCore().UseDbContext<ApplicationDbContext>();
}).AddServer(options =>
{
options.UseMvc();
options.EnableTokenEndpoint("/connect/token");
//options.EnableAuthorizationEndpoint("/connect/token");
// Enable the password flow.
options.AllowPasswordFlow();
// During development, you can disable the HTTPS requirement.
options.DisableHttpsRequirement();
}).AddValidation();
AuthorizationController.cs
[HttpPost("~/connect/token")]
[Produces("application/json")]
public async Task<IActionResult> Exchange(LoginViewModel loginVM)
{
if (loginVM.IsPasswordGrantType())
{
var user = await _accountManager.GetUserByPhoneAsync(loginVM.MobileNumber);
if (user == null)
{
return BadRequest(ResponeModel.Create("","",StatusCodes.UserNotFound,Messages.UserNotFound));
}
// Ensure the user is enabled.
if (user.IsDeleted || !user.IsEnabled)
{
return BadRequest(ResponeModel.Create("","",StatusCodes.UserAccountDisabled,Messages.UserAccountDisabled));
}
// Validate the username/password parameters and ensure the account is not locked out.
var result = await _signInManager.CheckPasswordSignInAsync(user, loginVM.Pin,true);
// Ensure the user is not already locked out.
if (result.IsLockedOut)
{
return BadRequest(ResponeModel.Create("","",StatusCodes.UserAccountSuspended,Messages.UserAccountSuspended));
}
if (!result.Succeeded)
{
return BadRequest(ResponeModel.Create("","",StatusCodes.InvalidLoginCredentials,Messages.InvalidLoginCrendentials));
}
// Create a new authentication ticket.
var ticket = await CreateTicketAsync(loginVM, user);
return SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme);
}
return BadRequest(ResponeModel.Create("","",StatusCodes.UnsupportedOpenIdGrantType,Messages.ErrorOccured, Messages.UnsupportedOpenidGrantType));
}
更多信息:
我正在使用邮差来测试端点。