azure-active-directory · 4: hello.js azure active directory b2c 10 examples 10 angularjs- azure...

16
azure-active-directory #azure- active- directory

Upload: others

Post on 28-May-2020

33 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

azure-active-directory

#azure-

active-

directory

Page 2: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

1

1: azure-active-directory 2

2

Examples 2

2

Azure Active Directory B2C - 2

Azure AD B2C 2

2: ADAL Cordova Plugin Azure Active Directory B2C 4

Examples 4

ADAL Cordova Plugin Azure Active Directory B2C 4

Azure B2C 4

- ADAL 4

3: Azure Active Directory B2C 7

7

Examples 7

Azure B2C - Angularjs ( ) 7

Azure B2C 7

- Hello.js 7

- ADAL 7

JWT 7

1. : 7

2. : 7

3. : 8

4. . 8

: 8

: 8

5. Azure AD B2C 8

: 9

9

9

: 9

Page 3: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

4: Hello.js Azure Active Directory B2C 10

Examples 10

Angularjs- Azure Active Directory B2C Hello.js 10

13

Page 4: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: azure-active-directory

It is an unofficial and free azure-active-directory ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official azure-active-directory.

The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected]

https://riptutorial.com/ko/home 1

Page 5: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

1: azure-active-directory azure-active-directory , azure-active-directory .

azure-active-directory . azure-active-directory .

Examples

azure-active-directory .

Azure Active Directory B2C -

Azure AD B2C

tenantName .

1.

Web App Native . /

URL http : // localhost : 8100 .

.

ID . clientId .

2.

. .3.

. .4.

Azure AD B2C

Azure AD B2C . UI

https://github.com/NewtonJoshua/Azure-ADB2C-Angularjs-sample .

UI .

B2C ansd : adCustomPages / unified.html•AD B2C : adCustomPages / resetpassword.html•

adCustomPages / unified.html 442 445 tenantName, password-reset-policy clientId .

BLOB URL Azure AD B2C .

Azure Blob .•AD URL .•Blob '*' ORIGINS CORS . . * .•

UI .

https://riptutorial.com/ko/home 2

Page 7: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

2: ADAL Cordova Plugin Azure Active Directory B2C

Examples

ADAL Cordova Plugin Azure Active Directory B2C

: https://github.com/NewtonJoshua/Azure-ADB2C-Angularjs-sample .

Azure B2C

Azure AD B2C . .

- ADAL

B2C ADAL Cordova Plugin Patch . Azure AD B2C Apache Cordova cordal-plugin-ms-adal ADAL (Active Directory ) . cordova-plugin-ms-adal Active Directory Apache Cordova .

angularjs / ionicframework .

:

cordova plugin add https://github.com/jospete/azure-activedirectory-library-for-cordova --save

bower install angular-jwt --save

LoginController .

.controller('LoginController', function($scope, $state, $ionicPopup, jwtHelper, AdalService) { $scope.login = function(){ AdalService.login().then(function(authResponse) { displayUserDetails(getUserData(authResponse)); }); $scope.logout = AdalService.logout; // Decode decode the token and diaplay the user details function getUserData(response) { var user = {}; user.token = response.access_token || response.token; var data = jwtHelper.decodeToken(user.token); user.expires_in = new Date(response.expires * 1000) || response.expiresOn; user.name = data.name; user.email = data.emails ? data.emails[0] : ''; user.id = data.oid; return user; }; function displayUserDetails(user) { $scope.user = user;

https://riptutorial.com/ko/home 4

Page 8: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

$ionicPopup.alert({ title: user.name, template: '<b>Email:</b> ' + user.email + '<br> <b>Id:</b> <code>' + user.id + '</code>' }); } });

Azure AD B2C .

.value('settings', { // ADAL-B2C configuration adalB2C: { tenantName: 'Enter your tenant name', clientId: 'Enter your client id', policy: 'Enter your policy name' } });

ADAL Azure AD B2C adal.service.

.advice ( 'AdalService', function ($ q, $ http, settings) {

var extraQueryParams = 'nux=1'; var userId = null; var redirectUri = 'https://login.microsoftonline.com/tfp/oauth2/nativeclient'; var authority = 'https://login.microsoftonline.com/' + settings.adalB2C.tenantName; var resourceUri = 'https://graph.windows.net'; this.login = function() { var deferredLoginResponse = $q.defer(); var authContext = new Microsoft.ADAL.AuthenticationContext(authority); // Attempt to authorize user silently authContext.acquireTokenSilentAsync(resourceUri, settings.adalB2C.clientId, userId, redirectUri, settings.adalB2C.policy) .then(function(authResponse) { deferredLoginResponse.resolve(authResponse); }, function() { // We require user credentials so triggers authentication dialog authContext.acquireTokenAsync(resourceUri, settings.adalB2C.clientId, redirectUri, userId, extraQueryParams, settings.adalB2C.policy) .then(function(authResponse) { deferredLoginResponse.resolve(authResponse); }, function(err) { deferredLoginResponse.reject(err); }); }); return deferredLoginResponse.promise; }; this.logout = function() { // Step1: clear cache var authContext = new Microsoft.ADAL.AuthenticationContext(authority); authContext.tokenCache.clear(); // Step2: make XmlHttpRequest pointing to the sign out url return $http.post(authority + '/oauth2/logout?post_logout_redirect_uri=' +

https://riptutorial.com/ko/home 5

Page 9: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

redirectUri); }; });

ADAL Cordova Plugin Azure Active Directory B2C : https://riptutorial.com/ko/azure-active-directory/topic/10770/adal-cordova-plugin--azure-active-directory-b2c-

https://riptutorial.com/ko/home 6

Page 10: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

3: Azure Active Directory B2CAzure AD B2C . .

Examples

Azure B2C - Angularjs ( )

AngularJS AD B2C .

https://github.com/NewtonJoshua/Azure-ADB2C-Angularjs-sample .

Azure B2C

Azure AD B2C . .

- Hello.js

Azure AD B2C ID Hello.js . Hello.js OAuth2 REST API JavaScript SDK.

- ADAL

B2C ADAL Cordova Plugin Patch . Azure AD B2C Apache Cordova cordal-plugin-ms-adal ADAL (Active Directory ) . cordova-plugin-ms-adal Active Directory Apache Cordova .

JWT

jwt JWT (JWT) . JSON Web Token RFC 7519 .

1. :

.

git clone https://github.com/NewtonJoshua/Azure-ADB2C-Angularjs-sample.git

1.

npm install

bower install

2.

2. :

ADAL-B2C

Azure AD B2C

tenantName .

1.

2.

https://riptutorial.com/ko/home 7

Page 11: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

Web App Native . /

URL http : // localhost : 8100 .

.

ID . clientId .

. .3.

. .4.

3. :

settings.value.js .

tenantName : 2.1 •clientId : 2.2 ID•: 2.3 •

4. .

:

ionic serve

:

1.

cordova platform add android

cordova platform add ios

2.

ionic cordova resources

3.

cordova build

Cordova , Android iOS .

5. Azure AD B2C

Azure AD B2C . UI

UI .

B2C ansd : adCustomPages / unified.html•

https://riptutorial.com/ko/home 8

Page 12: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

AD B2C : adCustomPages / resetpassword.html•

adCustomPages / unified.html 442 445 tenantName, password-reset-policy clientId .

BLOB URL Azure AD B2C .

Azure Blob .•AD URL .•Blob '*' ORIGINS CORS . . * .•

UI .

:

.

:

bower install ng-hello --save

bower install angular-jwt --save

hello.service.js .

:

cordova plugin add https://github.com/jospete/azure-activedirectory-library-for-cordova --save

bower install angular-jwt --save

adal.service.js .

:

: https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-overview1.

Azure AD - Azure AD AngularJS . https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-angular

2.

Azure AD B2C : OAuth 2.0 https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-spa

3.

Azure Active Directory B2C : https://riptutorial.com/ko/azure-active-directory/topic/10768/azure-active-directory-b2c

https://riptutorial.com/ko/home 9

Page 13: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

4: Hello.js Azure Active Directory B2C

Examples

Angularjs- Azure Active Directory B2C Hello.js

https://github.com/NewtonJoshua/Azure-ADB2C-Angularjs-ample .

Azure AD B2C ID Hello.js . Hello.js OAuth2 REST API JavaScript SDK.

jwt JWT (JWT) . JSON Web Token RFC 7519 .

angularjs .

LoginController .

.controller('LoginController', function($scope, $state, $ionicPopup, jwtHelper, HelloService) { // Initialize (function initialize() { HelloService.initialize().then(function(authResponse) { displayUserDetails(getUserData(authResponse)); }); })(); $scope.login = HelloService.login; $scope.logout = HelloService.logout; // Decode decode the token and display the user details function getUserData(response) { var user = {}; user.token = response.access_token || response.token; var data = jwtHelper.decodeToken(user.token); user.expires_in = new Date(response.expires * 1000) || response.expiresOn; user.name = data.name; user.email = data.emails ? data.emails[0] : ''; user.id = data.oid; return user; }; function displayUserDetails(user) { $scope.user = user; $ionicPopup.alert({ title: user.name, template: '<b>Email:</b> ' + user.email + '<br> <b>Id:</b> <code>' + user.id + '</code>' }); } });

Azure AD B2C .

https://riptutorial.com/ko/home 10

Page 14: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

.value('settings', { // ADAL-B2C configuration adalB2C: { tenantName: 'Enter your tenant name', clientId: 'Enter your client id', policy: 'Enter your policy name' } });

Hello.js Azure AD B2C hello.service .

.service('HelloService', function(hello, $q, settings) { var network = 'adB2CSignInSignUp'; this.initialize = function() { //initiate all policies hello.init({ adB2CSignIn: settings.adalB2C.clientId, adB2CSignInSignUp: settings.adalB2C.clientId, adB2CEditProfile: settings.adalB2C.clientId }, { redirect_uri: '../', scope: 'openid ' + settings.adalB2C.clientId, response_type: 'token id_token' }); var adB2CSignInSignUpPolicy = getPolicyConfiguredData(); hello.init(adB2CSignInSignUpPolicy); var authResponse = hello(network).getAuthResponse(); if (authResponse && !authResponse.error) { return $q.when(authResponse); } else { var error = authResponse && authResponse.error ? authResponse.error : ''; return $q.reject(error); } }; this.login = function() { hello(network).login({ display: 'page', force: true }); }; this.logout = function() { hello(network).logout({ force: true }); }; function getPolicyConfiguredData() { var adB2CSignInSignUpPolicy = {}; adB2CSignInSignUpPolicy[network] = { name: 'Azure Active Directory B2C', oauth: { version: 2, auth: 'https://login.microsoftonline.com/tfp/' + settings.adalB2C.tenantName + '/' + settings.adalB2C.policy + '/oauth2/v2.0/authorize', grant: 'https://login.microsoftonline.com/tfp/' + settings.adalB2C.tenantName

https://riptutorial.com/ko/home 11

Page 15: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

+ '/' + settings.adalB2C.policy + '/oauth2/v2.0/token' }, refresh: true, scope_delim: ' ', // Don't even try submitting via form. // This means no POST operations in <=IE9 form: false }; adB2CSignInSignUpPolicy[network].xhr = function(p) { if (p.method === 'post' || p.method === 'put') { //toJSON(p); if (typeof(p.data) === 'object') { // Convert the POST into a javascript object try { p.data = JSON.stringify(p.data); p.headers['content-type'] = 'application/json'; } catch (e) {} } } else if (p.method === 'patch') { hello.utils.extend(p.query, p.data); p.data = null; } return true; }; adB2CSignInSignUpPolicy[network].logout = function() { //get id_token from auth response var id_token = hello(network).getAuthResponse().id_token; //clearing local storage session hello.utils.store(network, null); //redirecting to Azure B2C logout URI window.location = ('https://login.microsoftonline.com/' + settings.adalB2C.tenantName + '/oauth2/v2.0/logout?p=' + settings.adalB2C.policy + '&id_token_hint=' + id_token + '&post_logout_redirect_uri=https://login.microsoftonline.com/' + settings.adalB2C.tenantName + '/oauth2/logout'); }; return adB2CSignInSignUpPolicy; } });

Hello.js Azure Active Directory B2C : https://riptutorial.com/ko/azure-active-directory/topic/10771/hello-js--azure-active-directory-b2c-

https://riptutorial.com/ko/home 12

Page 16: azure-active-directory · 4: Hello.js Azure Active Directory B2C 10 Examples 10 Angularjs- Azure Active Directory B2C Hello.js 10 13

S. No

Contributors

1azure-active-directory

Community, Newton Joshua

2ADAL Cordova Plugin Azure Active Directory B2C

Newton Joshua

3Azure Active Directory B2C

Newton Joshua

4Hello.js Azure Active Directory B2C

Newton Joshua

https://riptutorial.com/ko/home 13