Authentication 通过Azure移动服务和cordova应用程序使用自定义验证器进行身份验证
我正在使用Cordova/Phonegap和Azure移动服务后端构建一个手机应用程序。我需要使用自定义验证器,因为它与当前的软件产品集成 我使用来自的信息构建了服务器端 我设置Authentication 通过Azure移动服务和cordova应用程序使用自定义验证器进行身份验证,authentication,azure-mobile-services,Authentication,Azure Mobile Services,我正在使用Cordova/Phonegap和Azure移动服务后端构建一个手机应用程序。我需要使用自定义验证器,因为它与当前的软件产品集成 我使用来自的信息构建了服务器端 我设置config.SetIsHosted(true)在WebApiConfig 从客户那里,我似乎可以很好地进行身份验证并取回一个有效的令牌,但我不确定该如何处理它。我已尝试设置client.currentUser,但我的api调用返回为未经授权 下面是一些代码: client = new WindowsAzure.Mobi
config.SetIsHosted(true)代码>在WebApiConfig
从客户那里,我似乎可以很好地进行身份验证并取回一个有效的令牌,但我不确定该如何处理它。我已尝试设置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移动服务数据库中创建用户。我假设您正在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; }
}
受保护的覆盖异步无效OnNavigatedTo(NavigationEventArgs e)
{
LoginInfo dude=new LoginInfo(){username=“user”,password=“P-Dub”};
//等待App.MobileService.InvokeApiAsync(“CustomRegistration”,dude);
//调用自定义API以登录
var result=wait App.MobileService.InvokeApiAsync(“CustomLogin”,dude);
//创建用户凭据。
PasswordCredential credential=新的PasswordCredential(“UserToDo”,
result.user.userId、result.authenticationToken);
MobileServiceUser=新的MobileServiceUser(凭证.用户名);
credential.RetrievePassword();
user.MobileServiceAuthenticationToken=凭证.Password;
//从存储的凭据设置用户。
App.MobileService.CurrentUser=用户;
wait RefreshTodoItems();//这将访问移动服务。
}
公共类登录信息
{
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
}
公共类登录结果
{
公共用户{get;set;}
公共字符串身份验证令牌{get;set;}
}
公共类用户
{
公共字符串用户标识{get;set;}
}
Azure移动服务不支持自定义身份提供商。只有微软、Facebook、Twitter、谷歌和Active Directory。您正在尝试使用什么身份验证?自定义身份提供程序。。。正如我在条目顶部的链接中所讨论的,如果在登录后调用API会发生什么?不要设置任何用户。我认为客户端在调用API时会将其存储为aaccess。因此,除非您想缓存用户令牌,否则您不必做任何事情。在这种情况下,您可以执行此处建议的操作: