使用WCF的Ionic自定义身份验证端点文档/示例

3
我正在寻找有关Ionic自定义身份验证端点所需详细说明的更多信息。Ionic Custom Authentication。如果可能,请提供示例项目或教程以供参考。我已经进行了广泛的搜索,但是要么我的谷歌搜索技能已经消失,要么没有更多信息可以获取(据我所见)。
我已经使用“basic”设置进行了测试,但需要通过Ionic连接自己的端点。如下所示:
    Ionic.Auth.login('custom', options, details).then(function (success) {
        console.log('Ionic Login Success:');
        console.log(success);
    }, function (failure) {
        console.error('Ionic Login Failure: ' + failure.response.body.error.message);
    });

一些背景: 我有一个Ionic Cordova应用程序,可以通过某种登录/身份验证过程连接/验证到我的WCF Web API,然后检索已登录用户的数据。我需要将其转换为Ionic Auth。 PS. 我没有JWT的经验,并且已在我的WCF服务上安装了System.IdentityModel.Tokens.Jwt包。
感谢您的任何信息。 提前致谢!
引用: 我知道问题缺少一些细节,但在我了解更多之前,我无法提供更多信息。
1个回答

2
Ionic团队添加了三个示例项目,演示自定义身份验证后端。这三个示例是expressflaskgolang。我通过查看后端的服务器部分来设置了CakePHP 2.6.x的后端。
你应该能够组合出一些东西,因为这不是魔术。我添加了一些PHP片段,应该可以向你展示如何在特定的语言中完成它:
  • 从URL中获取redirect_uri参数并保存它
  • 从URL中获取state参数并保存它
  • 从URL中获取token参数并保存它
  • 将共享密钥保存到变量中
  • 使用HS256算法解码JWT令牌
解码后的令牌结构应该像这样:
"decoded_array": {
   "data": {
     "password": "someString",
     "email": "some@email.com"
   },
   "exp": 1457731231,
   "app_id": "your app id"
}
  • Now check if you have a user in your database (do the authentication)
  • If authentication was not successful, return a UnauthorizedException (401)
  • If the authentication was successful, create a new variable called payload. This should contain your user_id from your database:

    $payload = array('user_id' => $user['User']['id']);
    
  • Now encode payload with your sharedSecret string by creating a new JWT token:

$outgoingToken = JWT::encode($payload, $sharedSecret); (注:该文本包含代码部分,请注意格式转换。)
检查这个Github项目:https://github.com/driftyco/custom-auth-examples 你也可以在这里检查我的PHP实现:https://www.doonot.com/custom-auth-backend-for-ionic-auth-using-cakephp/

1
我按照您的教程操作,顺便说一下,教程非常好。我使用了php header("Location: $url")进行重定向 - 但问题在于我也被重定向到应用程序中。因此,在应用程序中,我也看到了"message": "You have successfully implemented custom authentication!"的提示信息。 - Mazz
2
只需从“url”中删除“&redirect_uri=https://api.ionic.io/auth/integrations/custom/success”。这仅用于调试。删除后,您应该被重定向到您的应用程序。 - nimrod
1
@doonot 当返回401进行身份验证错误时,它似乎只是打开应用内浏览器并且什么都不做。有什么想法吗? - MattSull
能否使用Laravel获取一个? - ballerz
@ballerz 我在我的问题中发布了 Laravel 3 控制器方法的一部分,类似于 MattSull 描述的问题:http://stackoverflow.com/questions/43346272/how-to-handle-login-errors-with-ionic-custom-auth - pwagner

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接