C# Xamarin、Azure、customauth和传递参数
我正在使用带有C#后端的Xamarin.Forms重写我们的应用程序,我正在尝试在登录时使用customauth。我已经让它工作到一定程度,但我正在努力从后端将我想要的一切传递回Xamarin应用程序。我正在获取令牌和用户id,但需要更多 成功登录时的后端代码似乎相对简单:C# Xamarin、Azure、customauth和传递参数,c#,azure,xamarin,xamarin.forms,custom-authentication,C#,Azure,Xamarin,Xamarin.forms,Custom Authentication,我正在使用带有C#后端的Xamarin.Forms重写我们的应用程序,我正在尝试在登录时使用customauth。我已经让它工作到一定程度,但我正在努力从后端将我想要的一切传递回Xamarin应用程序。我正在获取令牌和用户id,但需要更多 成功登录时的后端代码似乎相对简单: return Ok(GetLoginResult(body)); 其中GetLoginResult()是: LoginResult类是 public class LoginResult { public Logi
return Ok(GetLoginResult(body));
其中GetLoginResult()是:
LoginResult类是
public class LoginResult
{
public LoginResult(accounts account)
{
Response = 200;
CustomerId = account.CustomerId;
Modules = account.Modules;
User = new LoginResultUser
{
userId = account.id,
UserName = account.UserName,
EmployeeId = account.EmployeeId
};
}
[JsonProperty(PropertyName = "Response")]
public int Response { get; set; }
等
在应用程序中,我按如下方式调用customauth:
MobileServiceUser azureUser = await _client.LoginAsync("custom", JObject.FromObject(account));
结果具有令牌和正确的userid,但如何使用后端传回的其他属性填充结果?我已经让后端工作并使用postman进行了测试,我在那里得到的结果是我想要的,但我一直无法找到如何在应用程序中反序列化它。正如我所知,对于自定义身份验证,
MobileServiceClient.LoginAsync
将调用https://{your app name}.azurewebsites.net/.auth/login/custom
。使用时,您会发现此方法仅从响应中检索user.userId
和authenticationToken
,以构建MobileServiceClient
的CurrentUser
。据我所知,您可以在用户成功登录后利用MobileServiceClient.InvokeApiAsync
检索其他用户信息。此外,您还可以尝试遵循这一点寻找其他可能的方法
更新
您可以使用InvokeApiAsync
而不是LoginAsync
直接调用自定义登录端点,然后检索响应并获取附加参数,如下所示:
MobileServiceUser azureUser = await _client.LoginAsync("custom", JObject.FromObject(account));
成功登录后,我添加了一个新属性userName
,并对客户端做出如下响应:
MobileServiceUser azureUser = await _client.LoginAsync("custom", JObject.FromObject(account));
对于客户端,我添加了一个用于日志记录的自定义扩展方法,并按如下方式检索附加参数:
MobileServiceUser azureUser = await _client.LoginAsync("custom", JObject.FromObject(account));
以下是代码片段,您可以参考它们:
MobileServiceLoginExtend.cs
public static class MobileServiceLoginExtend
{
public static async Task CustomLoginAsync(this MobileServiceClient client, LoginAccount account)
{
var jsonResponse = await client.InvokeApiAsync("/.auth/login/custom", JObject.FromObject(account), HttpMethod.Post, null);
//after successfully logined, construct the MobileServiceUser object with MobileServiceAuthenticationToken
client.CurrentUser = new MobileServiceUser(jsonResponse["user"]["userId"].ToString());
client.CurrentUser.MobileServiceAuthenticationToken = jsonResponse.Value<string>("authenticationToken");
//retrieve custom response parameters
string customUserName = jsonResponse["user"]["userName"].ToString();
}
}
你好,布鲁斯。谢谢你的回复。你链接到的那篇文章就是我关注的那篇。我想你说的是,除了用户ID和令牌之外,loginAsync不能用于传回任何内容?是的,我已经反编译了库并跟踪了
MobileServiceClient.loginAsync
方法。我找到了解决方案,你可以参考我的更新。谢谢Bruce!我会试试看,然后好好享受一下。谢谢你,布鲁斯!