gpt4 book ai didi

c# - Azure移动服务.NET后端HttpResponseMessage返回null内容

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:36 26 4
gpt4 key购买 nike

我在移动服务的 Web api 中声明了此函数

public HttpResponseMessage Post(LoginRequest loginRequest)
{
EasyParkContext context = new EasyParkContext();
User user = context.Users.SingleOrDefault(a => a.UserName == loginRequest.UserName);
if (user != null)
{
if (BCrypt.Net.BCrypt.Verify(loginRequest.Password, user.Password))
{
ClaimsIdentity claimsIdentity = new ClaimsIdentity();
claimsIdentity.AddClaim(new Claim(ClaimTypes.PrimarySid, user.Id));
claimsIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, loginRequest.UserName));
LoginResult loginResult = new EasyParkLoginProvider(handler).CreateLoginResult(claimsIdentity, Services.Settings.MasterKey);
return this.Request.CreateResponse(HttpStatusCode.OK, loginResult);
}
}
return this.Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid username or password");
}

这个函数在我的客户端中声明

public async Task<bool> Login(string userName, string password)
{
LoginRequest loginRequest = new LoginRequest() { UserName = userName, Password = password };
try
{
HttpResponseMessage loginResult = await _service.InvokeApiAsync<LoginRequest, HttpResponseMessage>("EasyParkLogin", loginRequest);
JObject json = JObject.Parse(loginResult.Content.ToString());
_service.CurrentUser = new MobileServiceUser(json["user"]["userId"].ToString().Replace("EasyPark:", ""))
{
MobileServiceAuthenticationToken = json["authenticationToken"].ToString()
};
return true;
}
catch (Exception e)
{
return false;
}
}

它在移动服务页面上工作,但在我的客户端代码期间返回 null,抱歉,由于我的声誉,我无法附加图像...

最佳答案

public async Task<bool> Login(string userName, string password)
{
LoginRequest loginRequest = new LoginRequest() { UserName = userName, Password = password };
try
{
var loginResult = await _service.InvokeApiAsync("EasyParkLogin", JToken.FromObject(loginRequest));
JObject json = JObject.Parse(loginResult.ToString());
_service.CurrentUser = new MobileServiceUser(json["user"]["userId"].ToString().Replace("EasyPark:", ""))
{
MobileServiceAuthenticationToken = json["authenticationToken"].ToString()
};
return true;
}
catch (Exception e)
{
return false;
}
}

通过更改代码:

HttpResponseMessage loginResult = await _service.InvokeApiAsync<LoginRequest, HttpResponseMessage>("EasyParkLogin", loginRequest);
JObject json = JObject.Parse(loginResult.Content.ToString());

var loginResult = await _service.InvokeApiAsync("EasyParkLogin", JToken.FromObject(loginRequest));
JObject json = JObject.Parse(loginResult.ToString());

关于c# - Azure移动服务.NET后端HttpResponseMessage返回null内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29949617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com