2017-02-27 5 views
1

두 가지 시나리오를 시도합니다. 1. 리디렉션은 외부 사이트에서 내 사이트 페이지 중 하나에 발생할 수 있습니다. 이 리디렉션 후에 document.referrer를 사용하여 외부 사이트 이름을 캡처하려하지만 비어 있습니다. [참고 - Azure AD 인증 후 리디렉션되었습니다.]각도 2에서 document.referrer는 항상 공백입니다

  1. 내 사이트 (인증 후 - 삭제). 한 페이지에서 다른 페이지로 리디렉션합니다. 그리고 다음 페이지에서 document.referrer로 이전 페이지 URL을 얻으려고합니다. 하지만 여기서도 다시 비어 있습니다.

각도 2 타이프 스크립트를 사용하여 현재 페이지로 리디렉션이 발생한 위치에서 이전 페이지를 달성하는 방법을 제안하십시오.

답변

0

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"; 
    } 
} 

이 도움이 있으면 알려 주시기 바랍니다.

+0

답변 해 주셔서 감사합니다.하지만 서버 측에 인증 코드가 있습니다. Owin (startup.auth.cs) – namrata

1

AzureAD가 다시 리디렉션 된 후 state 변수에 리퍼러를 넣고 다시 읽으십시오.

상태 변수 및 보낼 수있는 다른 매개 변수에 대한 정보는 this doc을 확인하십시오.

관련 문제