通过Azure的移动服务和应用科尔多瓦定制认证认证 [英] Authentication with custom authenticator via Azure Mobile Services and cordova app
问题描述
我建立使用科尔多瓦/ PhoneGap的和的Azure移动服务后端一个手机应用程序。我需要使用,因为这与当前的软件产品整合了自定义的验证器。
我建立使用从<一个信息服务器端href=\"http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-get-started-custom-authentication/\" rel=\"nofollow\">http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-get-started-custom-authentication/
我设置 config.SetIsHosted(真);
在 WebApiConfig
从客户端,我似乎验证罚款和取回一个有效的道理,但我不知道该怎么办。我试过设置 client.currentUser
,但我的API调用都回来了未经授权的。
下面是一些code:
客户端=新WindowsAzure.MobileServiceClient(HTTP://`localhost`:50523);
登录名=函数(){
client.invokeApi(CustomLogin,{
方法:后
正文:{用户名:USER1,密码:PASS1}
})
.done(功能(结果){
VAR登录= JSON.parse(result.response);
client.currentUser = login.user;
client.currentUser.mobileServiceAuthenticationToken = login.authenticationToken; //现在可以尝试,我很有效
getMembers(); app.navigate(意见/ /home.html的);
},功能(错误){
警报(错误);
});}; getMembers =功能(){
client.invokeApi(会员,{
方法:获取,
身体: {}
})
.done(
功能(结果){
警报(result.result [0] .lName);
},
功能(错误){
警报(错误);
}); };
时有更多我需要身份验证令牌使这项工作呢?
谢谢!
编辑:一些更多的信息 -
使用招来监控流量,我看到调用 / API /成员
有几个头设置:
X-谟-AUTH:设置为我回到先前的令牌
的X谟-特点:AJ
的X谟安装-ID:一个GUID
的X谟-VERSION:谟/ 1.2(...)
确定。详细在这看着。首先,将样品仅产生在局部天青移动服务DB中的用户。我假设你正在创建在Azure中的实际移动服务的用户。你可以看到code的我评论如下,如果你想测试可以增加它的行。但这里是code应该做你想要什么。对不起,这是C#,而是应该翻译很容易。注意UserToDo是字符串我CustomLoginProvider.ProviderName回报。
保护覆盖异步无效的OnNavigatedTo(NavigationEventArgs E)
{
LoginInfo类花花公子=新LoginInfo类(){用户名=用户,密码=P-DUB}; //等待App.MobileService.InvokeApiAsync&LT;将LoginInfo,LoginResult&GT;(CustomRegistration花花公子); //调用自定义API登录
VAR的结果=等待App.MobileService.InvokeApiAsync&LT;将LoginInfo,LoginResult&GT;(CustomLogin花花公子); //创建用户凭据。
PasswordCredential证书=新PasswordCredential(UserToDo
result.user.userId,result.authenticationToken); MobileServiceUser用户=新MobileServiceUser(credential.UserName);
credential.RetrievePassword();
user.MobileServiceAuthenticationToken = credential.Password; //从存储的凭据设置用户。
App.MobileService.CurrentUser =用户;
等待RefreshTodoItems(); //这个访问移动服务。
}
公共类LoginInfo类
{
公共字符串的用户名{搞定;组; }
公共字符串密码{搞定;组; }
}
公共类LoginResult
{
公共用户用户{搞定;组; }
公共字符串authenticationToken {搞定;组; }
}公共类用户
{
公共字符串userid {搞定;组; }
}
I am building a phone app using Cordova/Phonegap and an Azure Mobile Services backend. I need to use a custom authenticator since this is integrating with a current software product.
I built the server side using info from http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-get-started-custom-authentication/
I set config.SetIsHosted(true);
in WebApiConfig
From the client, I seem to be authenticating fine and getting back a valid token, but I'm not sure what to do with it. I've tried setting client.currentUser
, but my api calls are coming back as unauthorized.
Here's some code:
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);
});
};
Is there more I need to do with the authentication token to make this work? Thanks!
EDIT: Some more info --
Using Fiddler to monitor the traffic, I see that the call to /api/member
has a few headers set:
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(...)
OK. Looked at this in detail. First, the sample is only creating users in the local Azure mobile service DB. I am assuming you are creating a user on the actual Mobile Service in Azure. You can see the line of code I commented out below that can add it if you want to test. But here is code that should do what you want. Sorry it is C#, but should translate easy enough. Note "UserToDo" is the string my CustomLoginProvider.ProviderName returns.
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; }
}
这篇关于通过Azure的移动服务和应用科尔多瓦定制认证认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!