2017-02-16 1 views
1

ASP.net 웹 API 2를 백엔드로 사용하고 이오니아 2 앱을 프런트 엔드로 사용하고 있습니다.asp.net 웹 API로 Ionic 2 앱 인증

저는 Microsoft의 응용 프로그램 인증에 UserStore Manager를 사용하고 있습니다.

그것은 토큰 기반 인증을 사용합니다 (잘못된 이름을 사용하고 있다면 용서해주세요.

저는 우편 배달부를 통해 토큰을 가져 와서이 웹 페이지에서 데이터를 가져 오기 위해 "Bearer [token]"과 같은 요청 헤더에이 토큰을 사용할 수있었습니다.

Getting the Token with Postman

당신이 방법을 볼 수 있듯이 포스트이며, 콘텐츠 형식은 x-www-form-urlencoded를합니다.

이렇게하면 access_token을 생성 한 다음이 토큰을 헤더에 사용하여 필요한 것을 얻습니다. 내 이온 앱에서

나는 공용 메서드 호출 로그인()

public login(credentials) { 
if (credentials.username === null || credentials.password === null) { 
    let error = this.alertController.create({ 
    title:'Lol', 
    message:'incomplete credentials, Fill them up and come back', 
    buttons: ['ok'] 
    }); 

    error.present(); 
} 
else { 
    return new Promise(resolve => { 
    this.http.post(`${this.baseUrl}/token`, 
    { 
     grant_type:'password', 
     username:credentials.username, 
     password:credentials.password 
    }).map(res => console.log(resolve(res.json()))); 
    }); 
} 

}

그리고 여기에이 메소드를 호출 circles.ts의 코드와 함께 인증-service.ts을 만들어 :

login(){ 
let credentials = { 
    username:"01286220336", 
    password:"[email protected]$$w0rd" 
} 
this.authService.login(credentials) 
.then(data => console.log(data)) 
.catch(data => console.log(data)); 

}

내가 Circles.ts에 로그인 방법을 호출 할 때 내 문제가 아니오 나는 왜 그런지 모릅니다. 나는 2 주 동안 노력했지만 방금 포기했다.

또한 authservice.ts에서 login 메소드를 구현하고 있는지 잘 모르겠습니다.

결국 나는 어떻게 든이 토큰을 얻을 필요가있다. 누군가가 나를 올바른 방법으로 넣을 수 있다면 나는 매우 감사 할 것이다 !!

+0

문제는 형식입니다. 올바른 형식 (x-www-form-url로 인코딩 됨)으로 보내려면 로그인 헤더를 json ..으로 보내야합니다. username = xxxx & password = xxxx & grant_type = password –

+0

새 약속을 반환하십시오. (해결 => { this.http.post ('$ {this.baseUrl}/토큰', ) 사용자 이름/암호/grant_type과 같은 사용자 이름/$ {credentials.username} & password = $ {credentials.password} & grant_type = password' ) .map (res => resolve (res.json())); }}); 새 스 니펫은 여전히 ​​아무것도 반환하지 않습니다. – Willy

+0

게시물의 헤더를 건너 뛰기를 기다립니다. this.http.post ($ {this.baseUrl}/token, username = $ {credentials.username} & password = $ {credentials.pass word} & grant_type = pas 검, {headers : { '내용 유형 :' 'x-www-form-urlencoded'}}) –

답변

0

else 부분을 다음 코드로 바꾸어보십시오.

else{ 
    let authObject = "username=xxxx&password=xxxx&grant_type=password"; 
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }); 
    headers.append('Accept', 'application/json'); 
    let options = new RequestOptions({ headers: headers }); 
    return this.http.post(this.baseUrl + '/token', authObject, options) 
     .toPromise() 
     .then(res => { 
      alert(JSON.stringify(res.json(), null, 4)); 
     }); 
}