我在Web API项目中使用JWT令牌进行身份验证。 我可以成功登录到我的帐户。 我试图找出要在Postman中为我的文档设置的请求参数。
我发送的从用户界面获取令牌的请求如下所示
public getToken(parameter: string[]): Observable<any> {
const data = new HttpParams()
.set('username', parameter[0])
.set('password', parameter[1])
.set('grant_type', 'password');
const url = '/api/login';
return this._httpClient.post(url, data.toString()).pipe(
map(x => x),
catchError((error: any) => {
let errorMessage: any;
if (error.error && error.error.error_description) {
errorMessage = error.error.error_description;
}
throw errorMessage;
})
);
}
在API中,代码是这样的
public class ldAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
string userName = CryptoEngine.GetDecryptedEmail(context.UserName);
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
using (SelfMeEntities scContext = new SelfMeEntities())
{
//string encryptedPassword = CryptoEngine.EncryptPassword(context.Password);
var user = scContext.Users.SingleOrDefault(b => b.EmailAddress == userName && b.OTP == context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
var identity = new ClaimsIdentity("JWT");
identity.AddClaim(new Claim("userId", user.ID.ToString(), ClaimValueTypes.Integer));
identity.AddClaim(new Claim("userName", user.FirstName.ToString()));
identity.AddClaim(new Claim("userEmail", user.EmailAddress.ToString()));
context.Validated(new AuthenticationTicket(identity, null));
}
}
}
我正在使用的当前参数是
方法:POST 网址:http://localhost:60067/api/login
授权中 类型:不记名令牌
标题中
授权:不记名[我的令牌] 内容类型:文本/纯文本
体内
原始格式
“ username=dummy@dummy.com&password=310611&grant_type=password”
我不确定我缺少什么, 使用当前的设置,我可以使用api,但是我得到的是空值