【发布时间】:2020-11-11 21:53:36
【问题描述】:
我的函数 Verify 中有一个 Web 服务调用。
代码如下:
public async Task<ActionResult> Index()
{
....
UserContext.LoginUser = null;
if (!string.IsNullOrEmpty(etoken))
{
SSOLogin ssoLogin = new SSOLogin();
LoginUser user = await ssoLogin.Verify(etoken);
UserContext.LoginUser = user;
if (UserContext.LoginUser == null)
return RedirectToAction("UnAuthorized", "Login");
else
{
Session["authenticated"] = true;
userId = UserContext.LoginUser.UserId;
domain = UserContext.LoginUser.Domain;
}
}
}
public async Task<LoginUser> Verify(string etoken)
{
string publicKey = "xxx";
LoginUser loginUser = null;
WSVerifyAccessToken wsVerifyAccessToken = new WSVerifyAccessToken();
string verifyResponse = await wsVerifyAccessToken.VerifyAccessToken(etoken); // Inside, this is a web service call
if (!string.IsNullOrEmpty(verifyResponse))
{
/* Some processing here */
}
return loginUser;
}
问题是,Verify 函数后 UserContext 变为 null
LoginUser user = await ssoLogin.Verify(etoken);
因此当我分配时
UserContext.LoginUser = user;
给我错误System.NullReferenceException: Object reference not set to an instance of an object.。
我试图使函数同步
LoginUser user = await ssoLogin.Verify(etoken).Result;
但是 Web 服务调用会一直挂在那里并且永远不会结束。
有什么办法吗?
【问题讨论】:
标签: c# asp.net web-services