【问题标题】:How to fix context.password coming null in /Token request如何修复 context.password 在 /Token 请求中为空
【发布时间】:2017-12-07 10:03:02
【问题描述】:

类:启动

public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
            //enable cors origin requests
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

            var myProvider = new ClinicServerProvider();
            OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/api/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(30),
                Provider = myProvider
            };
            app.UseOAuthAuthorizationServer(options);
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

            HttpConfiguration config = new HttpConfiguration();
            WebApiConfig.Register(config);
        }
    }

类:ClinicServerProvider

 public class ClinicServerProvider : OAuthAuthorizationServerProvider
    {
        private readonly BoilerplateDbContext _context;

        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            UnitOfWork uow = new UnitOfWork(_context);
            string epass = Crypto.EncryptString(context.Password);
            var user = uow.UserRepository.Get(x => x.Password == epass && x.UserName == context.UserName && x.IsActive==true).FirstOrDefault();
// Other custom code here.

}
}

在上述方法中.. 我总是得到 context.Password 为空..

有人知道是什么原因吗?

【问题讨论】:

  • 你没有展示你实际上是如何调用那个 /token 的。
  • @Evk 我从邮递员那里打来电话,传递了访问详细信息。
  • @Evk snag.gy/ABWMdx.jpg 检查这个。以前工作,但不知道为什么它停止工作..现在密码为空。用户名来了。

标签: c# asp.net-web-api token asp.net-authorization


【解决方案1】:

在验证用户名密码后添加声明后,在方法 GrantResourceOwnerCredentials 上,您需要添加此声明:

identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));

【讨论】:

  • 我没有得到密码。像 context.username 填充正常。但 context.password 为空。
【解决方案2】:

一个迟来的答案,但万一将来有人可能会遇到类似的情况......

您的请求数据中的x-www-form-urlencoded 键名必须命名为username 和password。

如果键名不同,context.UserName 和 context.Password 将始终生成 null 值。

【讨论】:

    【解决方案3】:

    我遇到了与您类似的问题,但我的问题是context.UserName = ""。我努力检查我的代码,但没有发现任何错误。

    我测试邮递员的请求。我在 Postman 的参数中找到了问题的原因。 解决方法是删除参数username重新创建。

    Parameter on Postman

    我认为这个解决方案可能会对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 2021-09-29
      • 2016-09-01
      • 2012-12-29
      • 2020-03-17
      • 2017-06-08
      • 1970-01-01
      • 2020-11-10
      相关资源
      最近更新 更多