【问题标题】:Authentication with custom authenticator via Azure Mobile Services and cordova app通过 Azure 移动服务和 cordova 应用程序使用自定义身份验证器进行身份验证
【发布时间】:2014-12-08 08:57:39
【问题描述】:

我正在使用 Cordova/Phonegap 和 Azure 移动服务后端构建一个电话应用程序。我需要使用自定义身份验证器,因为它与当前的软件产品集成。

我使用来自http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-get-started-custom-authentication/ 的信息构建了服务器端

我在WebApiConfig中设置了config.SetIsHosted(true);

从客户端,我似乎可以很好地进行身份验证并取回一个有效的令牌,但我不确定如何处理它。我已尝试设置 client.currentUser,但我的 api 调用返回为未经授权。

这里有一些代码:

client = new WindowsAzure.MobileServiceClient("http://`localhost`:50523");      
login = function () {
    client.invokeApi("CustomLogin", {
    method: "post",
    body: { "username": "user1", "password": "pass1" }
    })
    .done(function (result) {
        var login = JSON.parse(result.response);
        client.currentUser = login.user;
        client.currentUser.mobileServiceAuthenticationToken = login.authenticationToken;

        //lets try now that i'm valid
        getMembers();

        app.navigate("views/home.html");
    }, function(error) {
        alert(error);
        });

};

    getMembers = function () {
        client.invokeApi("Member", {
            method: "get",
            body: {}
            })
        .done(
        function(result) {
            alert(result.result[0].lName);
            },
        function(error) {
            alert(error);
            });

    };

我还需要对身份验证令牌做更多的事情来完成这项工作吗? 谢谢!

编辑:更多信息 --

使用 Fiddler 监控流量,我看到对/api/member 的调用设置了几个标头:

X-ZUMO-AUTH: set to the token I got back earlier
X-ZUMO-FEATURES: AJ
X-ZUMO-INSTALLATION-ID: a guid
X-ZUMO-VERSION: ZUMO/1.2(...)

【问题讨论】:

  • Azure 移动服务不支持自定义标识提供程序。只有 Microsoft、Facebook、Twitter、Google 和 Active Directory。您尝试使用什么身份验证?
  • 自定义身份提供者...正如我在条目顶部的链接中所讨论的那样。
  • 登录后只调用API会发生什么?不要设置任何用户。我认为客户端在调用 API 时将其存储为访问。因此,除非您想缓存用户令牌,否则您不必做任何事情。在这种情况下,您可以执行此处的建议:azure.microsoft.com/en-us/documentation/articles/…

标签: authentication azure-mobile-services


【解决方案1】:

好的。详细看了这个。首先,该示例仅在本地 Azure 移动服务数据库中创建用户。我假设您正在 Azure 中的实际移动服务上创建用户。您可以看到我在下面注释掉的代码行,如果您想测试,可以添加它。但这里的代码应该做你想做的。抱歉,它是 C#,但应该很容易翻译。注意“UserToDo”是我的 CustomLoginProvider.ProviderName 返回的字符串。

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    LoginInfo dude = new LoginInfo() { username = "user", password = "P-Dub" };

    //  await App.MobileService.InvokeApiAsync<LoginInfo, LoginResult>("CustomRegistration", dude);

    //  Call Custom API To Log iN
    var result = await App.MobileService.InvokeApiAsync<LoginInfo,LoginResult>("CustomLogin", dude);

    // Create the user credentials.
    PasswordCredential credential = new PasswordCredential("UserToDo",
        result.user.userId, result.authenticationToken);

    MobileServiceUser user = new MobileServiceUser(credential.UserName);
    credential.RetrievePassword();
    user.MobileServiceAuthenticationToken = credential.Password;

    // Set the user from the stored credentials.
    App.MobileService.CurrentUser = user;


    await RefreshTodoItems();  // This accesses the mobile service.
}


public class LoginInfo
{
    public String username { get; set; }
    public String password { get; set; }
}


public class LoginResult
{
    public User user { get; set; }
    public string authenticationToken { get; set; }
}

public class User
{
    public string userId { get; set; }
}

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2015-10-06
    • 1970-01-01
    相关资源
    最近更新 更多