解决:String reference not set to an instance of a String.Parameter name: s

解决:String reference not set to an instance of a String.Parameter name: s

3060发表于2016-07-17

今天用JWT做登录token的时候出现下面错误:

String reference not set to an instance of a String.
Parameter name: s,参数:7B2hdIrNUPIYY0xD6GE6vhk0Se+buWpinm+/oc9bSEVd0QBUyyeFHO4C2+aO QRfI7OhCFFhiDlLUA5dKlokKvg/LoKa6jlMsOk6j+FPX8q/TXk0jrcHyxYc5 rohKaU6AXqLseKwb+8g=String reference not set to an instance of a String.
Parameter name: s, Exception=>   at System.Text.Encoding.GetBytes(String s)
   at JWT.JsonWebToken.Encode(Object payload, String key, JwtHashAlgorithm algorithm)
   at Lanhu.seoDayCharge.API.Core.TokenHelper.GenToken(String oid, String currentDevice) in e:\Stuff\LanhuseoDayCharged\Lanhu.seo.DayCharge.API.Core\TokenHelper.cs:line 19
   at Lanhu.seoDayCharge.DAL.MemberService.DoLogin(ReqLogin req) in e:\Stuff\LanhuseoDayCharged\Lanhu.seoDayCharge.DAL\MemberService.cs:line 35
   at Lanhu.seoDayCharge.API.Controllers.UserController.Login(ReqDesParams req) in e:\Stuff\LanhuseoDayCharged\Lanhu.seoDayCharge.API\Controllers\UserController.cs:line 28,   at System.Text.Encoding.GetBytes(String s)
   at JWT.JsonWebToken.Encode(Object payload, String key, JwtHashAlgorithm algorithm)
   at Lanhu.seoDayCharge.API.Core.TokenHelper.GenToken(String oid, String currentDevice) in e:\Stuff\LanhuseoDayCharged\Lanhu.seo.DayCharge.API.Core\TokenHelper.cs:line 19
   at Lanhu.seoDayCharge.DAL.MemberService.DoLogin(ReqLogin req) in e:\Stuff\LanhuseoDayCharged\Lanhu.seoDayCharge.DAL\MemberService.cs:line 35
   at Lanhu.seoDayCharge.API.Controllers.UserController.Login(ReqDesParams req) in e:\Stuff\LanhuseoDayCharged\Lanhu.seoDayCharge.API\Controllers\UserController.cs:line 28
---------------------------------------------------------
本地是好的,发到线上去就有问题了。

仔细对比了一下,原来是服务器上少加了一个配置jwtKey,导致:

下面行代码报错:


var token = JWT.JsonWebToken.Encode(payload, AppConfig.JWTKey, JWT.JwtHashAlgorithm.HS256);
由于服务器没有配置生成token的key(AppConfig.JWTKey),所以这个属性值为null。调用 JWT.JsonWebToken.Encode固然就异常了。



总结:

出现String reference not set to an instance of a String.Parameter name: s,首先检查一下参数是不是有null的情况。

小编蓝狐