Microsoft는 Microsoft Graph를 Angular2로 호출하는 데모 데모 코드 angular2-connect-rest-sample도 제공합니다.
이 코드 샘플에서는 Azure AD를 먼저 인증해야합니다. 따라서 Angular2에서 인증에 대한 코드를 참조 할 수 있습니다.
그러나 로그인 할 때 리디렉션 URL이 일치하지 않습니다 (). 아래 코드를 변경하면 코드 예제가 제대로 작동합니다.
authHelper.ts
import { Injectable } from "angular2/core";
import { SvcConsts } from "../svcConsts/svcConsts";
@Injectable()
export class AuthHelper {
//function to parse the url query string
private parseQueryString = function(url) {
var params = {}, queryString = url.substring(1),
regex = /([^&=]+)=([^&]*)/g, m;
while (m = regex.exec(queryString)) {
params[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}
return params;
}
private params = this.parseQueryString(location.hash);
public access_token:string = null;
constructor() {
//check for id_token or access_token in url
if (this.params["id_token"] != null)
this.getAccessToken();
else if (this.params["access_token"] != null)
this.access_token = this.params["access_token"];
}
// add this function to modify the redirect url using the base address
getRedrectUrl(){
return window.location.href.substr(0,window.location.href.indexOf("#"));
}
login() {
//redirect to get id_token
window.location.href = "https://login.microsoftonline.com/" + SvcConsts.TENANT_ID +
"/oauth2/authorize?response_type=id_token&client_id=" + SvcConsts.CLIENT_ID +
"&redirect_uri=" + encodeURIComponent(this.getRedrectUrl()) +
"&state=SomeState&nonce=SomeNonce";
}
private getAccessToken() {
//redirect to get access_token
window.location.href = "https://login.microsoftonline.com/" + SvcConsts.TENANT_ID +
"/oauth2/authorize?response_type=token&client_id=" + SvcConsts.CLIENT_ID +
"&resource=" + SvcConsts.GRAPH_RESOURCE +
"&redirect_uri=" + encodeURIComponent(this.getRedrectUrl()) +
"&prompt=none&state=SomeState&nonce=SomeNonce";
}
}
이 도움이 있으면 알려 주시기 바랍니다.
답변 해 주셔서 감사합니다.하지만 서버 측에 인증 코드가 있습니다. Owin (startup.auth.cs) – namrata