在Azure的移动服务注册和登录用户 [英] Registering and login users in Azure Mobile Services

查看:104
本文介绍了在Azure的移动服务注册和登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在以下这一系列关于移动服务以及我正在使用最新的实例教程。现在我想注册教学贯彻和Windows手机登录为例。我改变插入允许任何人以应用程序键,我可以将这个code新用户:

I was following this series about Mobile Services and I am using examples in latest tutorial. Now I want to impelement registering and login in Windows Phone for example. I changed to Insert permission to anyone with application key and I can Insert new user by this code:

await accountTable.InsertAsync(new accounts() { Username = "admin", Password = "mypassword" });

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

But I don't know how can I now check for login user? How to get token?

推荐答案

您提到在去年年底,被写的时候有是在Azure移动服务的自定义API不支持的职位 - 在这里你可以有唯一的地方对于用户调用执行的脚本是在表上。现在你应该实际使用该API的定制 - 在这里你可以定义两个API - 一个用于注册用户,另外一个用于登录。在客户端,当你调用登录时,API会验证用户名/密码,然后返回谟令牌,该客户端可随后设置为(与该博客帖子中的脚本创建)的currentUser MobileServiceClient 对象的属性。

The post you referred was written at the end of last year, when there was no support for custom APIs on Azure Mobile Services - the only place where you could have scripts executed for user calls were on tables. Nowadays you should actually use custom APIs for that - where you can define two APIs - one for registering the user, and another one for login. On the client, when you call login, the API would validate the user name / password, then return the Zumo token (created with the script shown in that blog post), which the client can then set to the CurrentUser property of the MobileServiceClient object.

类似下面的code:

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"];

和API看起来像下面的code:

And the API would look something like the code below:

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

这篇关于在Azure的移动服务注册和登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆