java动态令牌验证,Web API令牌身份验证

我将用户凭据从Web应用程序发布到web api,后者实现了对用户进行身份验证并使用有效令牌进行响应的提供程序 .

这是发布的方法:

public TokenModel RequestAPIToken(string username, string password)

{

var postData = new Dictionary();

postData.Add("grant_type", "password");

postData.Add("username ", username);

postData.Add("password ", password);

HttpContent content = new FormUrlEncodedContent(postData);

_response = _client.PostAsync("token", content).Result;

var result = _response.Content.ReadAsAsync().Result;

return result;

}

这取自web api项目:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)

{

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

var _userServices = new UserServices();

User user = _userServices.GetValidatedUser(context.UserName, context.Password).FirstOrDefault();

if (user == null)

{

context.SetError("invalid_grant", "The user name or password is incorrect.");

return;

}

var identity = new ClaimsIdentity(context.Options.AuthenticationType);

identity.AddClaim(new Claim("userId", user.UserId.ToString()));

identity.AddClaim(new Claim("username", user.Username.ToString()));

context.Validated(identity);

}

问题是context.UserName和context.Password总是为null!我尝试使用键值对而不是dictinary而我正在使用_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application / x-www-form-urlencoded”));

有什么建议吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值