Xamarin、Azure、customauth 和传递参数
Xamarin, Azure, customauth and passing parameters
我正在使用 Xamarin.Forms 和 C# 后端重写我们的应用程序,我正尝试在登录时使用 customauth。我已经让它工作到一定程度,但我正在努力将我想要的一切从后端传回 Xamarin 应用程序。我正在获取令牌和用户 ID,但还想要更多。
成功登录的后端代码看起来比较简单:
return Ok(GetLoginResult(body));
其中 GetLoginResult() 是:
private object GetLoginResult(IUser body)
{
var claims = new Claim[]
{
new Claim(JwtRegisteredClaimNames.Sub, body.username)
};
JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
claims, signingKey, audience, issuer, TimeSpan.FromDays(30));
accounts account = db.accounts.Single(u => u.username.Equals(body.username));
return new LoginResult(account)
{
authenticationToken = token.RawData,
};
}
并且登录结果 class 是
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));
结果具有令牌和正确的用户标识,但如何使用后端传回的附加属性填充结果?我已经让后端工作并使用邮递员进行了测试,我得到的结果是我想要的,但我一直无法找到如何在应用程序中反序列化它。
据我所知,对于自定义身份验证,MobileServiceClient.LoginAsync
会调用 https://{your-app-name}.azurewebsites.net/.auth/login/custom
。将 ILSPy you could find that this method would only retrieve the user.userId
and authenticationToken
from the response to construct the CurrentUser
of your MobileServiceClient
. Per my understanding, you could leverage MobileServiceClient.InvokeApiAsync
to retrieve the additional user info after the user has logged in successfully. Additionally, you could try to follow this toturial 用于其他可能的方法时。
更新
您可以使用 InvokeApiAsync
而不是 LoginAsync
直接调用自定义登录端点,然后检索响应并获取其他参数,如下所示:
登录成功后,我添加了一个新的属性 userName
并响应客户端如下:
对于客户端,我添加了一个自定义扩展方法来记录和检索附加参数,如下所示:
下面是代码片段,大家可以参考一下:
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();
}
}
登录处理
MobileServiceClient client = new MobileServiceClient("https://bruce-chen-002-staging.azurewebsites.net/");
var loginAccount = new LoginAccount()
{
username = "brucechen",
password = "123456"
};
await client.CustomLoginAsync(loginAccount);
我正在使用 Xamarin.Forms 和 C# 后端重写我们的应用程序,我正尝试在登录时使用 customauth。我已经让它工作到一定程度,但我正在努力将我想要的一切从后端传回 Xamarin 应用程序。我正在获取令牌和用户 ID,但还想要更多。
成功登录的后端代码看起来比较简单:
return Ok(GetLoginResult(body));
其中 GetLoginResult() 是:
private object GetLoginResult(IUser body)
{
var claims = new Claim[]
{
new Claim(JwtRegisteredClaimNames.Sub, body.username)
};
JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
claims, signingKey, audience, issuer, TimeSpan.FromDays(30));
accounts account = db.accounts.Single(u => u.username.Equals(body.username));
return new LoginResult(account)
{
authenticationToken = token.RawData,
};
}
并且登录结果 class 是
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));
结果具有令牌和正确的用户标识,但如何使用后端传回的附加属性填充结果?我已经让后端工作并使用邮递员进行了测试,我得到的结果是我想要的,但我一直无法找到如何在应用程序中反序列化它。
据我所知,对于自定义身份验证,MobileServiceClient.LoginAsync
会调用 https://{your-app-name}.azurewebsites.net/.auth/login/custom
。将 ILSPy you could find that this method would only retrieve the user.userId
and authenticationToken
from the response to construct the CurrentUser
of your MobileServiceClient
. Per my understanding, you could leverage MobileServiceClient.InvokeApiAsync
to retrieve the additional user info after the user has logged in successfully. Additionally, you could try to follow this toturial 用于其他可能的方法时。
更新
您可以使用 InvokeApiAsync
而不是 LoginAsync
直接调用自定义登录端点,然后检索响应并获取其他参数,如下所示:
登录成功后,我添加了一个新的属性 userName
并响应客户端如下:
对于客户端,我添加了一个自定义扩展方法来记录和检索附加参数,如下所示:
下面是代码片段,大家可以参考一下:
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();
}
}
登录处理
MobileServiceClient client = new MobileServiceClient("https://bruce-chen-002-staging.azurewebsites.net/");
var loginAccount = new LoginAccount()
{
username = "brucechen",
password = "123456"
};
await client.CustomLoginAsync(loginAccount);