【问题标题】:Xamarin, Azure, customauth and passing parametersXamarin、Azure、customauth 和传递参数
【发布时间】:2017-03-14 20:58:44
【问题描述】:

我正在使用带有 C# 后端的 Xamarin.Forms 重写我们的应用程序,并且我正在尝试在登录时使用 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,
            };
        }

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));

结果包含令牌和正确的用户 ID,但是如何使用后端传回的其他属性填充结果?我已经让后端工作并使用邮递员进行了测试,我得到的结果是我想要的,但我一直无法找到如何在应用程序中对其进行反序列化。

【问题讨论】:

    标签: c# azure xamarin xamarin.forms custom-authentication


    【解决方案1】:

    据我所知,对于自定义身份验证,MobileServiceClient.LoginAsync 将调用 https://{your-app-name}.azurewebsites.net/.auth/login/custom。使用ILSPy 时,您会发现此方法只会从响应中检索user.userIdauthenticationToken,以构造您的MobileServiceClientCurrentUser。据我了解,您可以在用户成功登录后利用MobileServiceClient.InvokeApiAsync 来检索其他用户信息。此外,您可以尝试关注此toturial 以了解其他可能的方法。

    更新

    您可以使用InvokeApiAsync 而不是LoginAsync 直接调用自定义登录端点,然后检索响应并获取附加参数,如下所示:

    登录成功后,我添加了一个新属性userName,并响应客户端如下:

    对于客户端,我添加了一个自定义扩展方法,用于记录和检索附加参数,如下所示:

    这里是sn-p的代码,大家可以参考一下:

    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);
    

    【讨论】:

    • 嗨布鲁斯。谢谢你的回复。您链接的文章是我关注的文章。我认为您的意思是 loginAsync 不能用于传回除用户 ID 和令牌以外的任何内容?
    • 是的,我已经反编译了库并跟踪了MobileServiceClient.LoginAsync方法。
    • 我找到了适合您的方案的解决方法,您可以参考我的更新。
    • 谢谢布鲁斯!我会试一试并报告
    • 辛苦了。谢谢布鲁斯!
    猜你喜欢
    • 2020-06-05
    • 2021-03-21
    • 2015-10-27
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多