在Azure移动服务中注册和登录用户

Lib*_*tal 6 authentication authorization azure windows-phone-8 azure-mobile-services

我正在关注有关移动服务的系列文章,我正在使用最新教程中的示例.现在我想在Windows Phone中注册和登录例如.我更改为使用应用程序密钥的任何人插入权限,我可以通过此代码插入新用户:

await accountTable.InsertAsync(new accounts() { Username = "admin", Password = "mypassword" });
Run Code Online (Sandbox Code Playgroud)

但我不知道我现在如何检查登录用户?如何获得令牌?

car*_*ira 5

您提到的帖子是在去年年底编写的,当时Azure Mobile Services上不支持自定义API - 唯一可以在桌面上执行用户调用脚本的地方.现在你应该实际使用自定义API - 你可以在其中定义两个API - 一个用于注册用户,另一个用于登录.在客户端,当您调用login时,API将验证用户名/密码,然后返回Zumo令牌(使用该博客文章中显示的脚本创建),然后客户端可以将其设置为对象的CurrentUser属性MobileServiceClient.

类似下面的代码:

var loginInput = new JObject();
loginInput.Add("userName", "theUserName");
loginInput.Add("password", "thePassword");
var loginResult = await client.InvokeApiAsync("login", loginInput);
client.CurrentUser = new MobileServiceUser((string)loginResult["user"]);
client.CurrentUser.MobileServiceAuthenticationToken = (string)loginResult["token"];
Run Code Online (Sandbox Code Playgroud)

API看起来像下面的代码:

exports.post = function(req, res) {
    var user = req.body.userName;
    var pass = req.body.password;
    validateUserNamePassword(user, pass, function(error, userId, token) {
        if (error) {
            res.send(401, { error: "Unauthorized" });
        } else {
            res.send(200, { user: userId, token: token });
        }
    });
}
Run Code Online (Sandbox Code Playgroud)