2016-01-08 66 views
-4

在angularjs如何加密和解密使用JSON令牌認證證書上基於令牌的認證工作。我使用Webapi和Mysql有沒有任何例子。如何使用Angularjs

回答

0

你可以使用angular-jwt module by auth0,它提供了一個decodeToken方法。

下面是它的文檔的爲例:

angular.module('app', ['angular-jwt']) 
.controller('Controller', function Controller(jwtHelper) { 
    var expToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tLyIsInN1YiI6ImZhY2Vib29rfDEwMTU0Mjg3MDI3NTEwMzAyIiwiYXVkIjoiQlVJSlNXOXg2MHNJSEJ3OEtkOUVtQ2JqOGVESUZ4REMiLCJleHAiOjE0MTIyMzQ3MzAsImlhdCI6MTQxMjE5ODczMH0.7M5sAV50fF1-_h9qVbdSgqAnXVF7mz3I6RjS6JiH0H8'; 

    var tokenPayload = jwtHelper.decodeToken(expToken); 
}) 

這裏是the js code for this method

angular.module('angular-jwt.jwt', []) 
.service('jwtHelper', function($window) { 

    this.urlBase64Decode = function(str) { 
     var output = str.replace(/-/g, '+').replace(/_/g, '/'); 
     switch (output.length % 4) { 
     case 0: { break; } 
     case 2: { output += '=='; break; } 
     case 3: { output += '='; break; } 
     default: { 
      throw 'Illegal base64url string!'; 
     } 
     } 
     return $window.decodeURIComponent(escape($window.atob(output))); //polyfill https://github.com/davidchambers/Base64.js 
    } 


    this.decodeToken = function(token) { 
     var parts = token.split('.'); 

     if (parts.length !== 3) { 
     throw new Error('JWT must have 3 parts'); 
     } 

     var decoded = this.urlBase64Decode(parts[1]); 
     if (!decoded) { 
     throw new Error('Cannot decode the token'); 
     } 

     return angular.fromJson(decoded); 
    } 


    }); 
+0

但怎樣才能加密和解密的憑據? – Jack

+0

好吧,我覺得你有點困惑,並試圖談論令牌。你能告訴我你到底想要做什麼嗎? – noelmace

+0

我想驗證用戶名密碼,但我想加密和解密數據http://jasonwatmore.com/post/2015/03/10/AngularJS-User-Registration-and-Login-Example.aspx – Jack