asp.net mvc webapi를 백엔드 및 클라이언트 측 라우팅 (cshtml 없음)으로 사용하는 SPA angularjs 응용 프로그램에서 인증 및 권한 부여 용 예제를 만들고 싶습니다. 다음은 완전한 예제를 설정하는 데 사용할 수있는 함수의 예입니다. 그러나 나는 그걸 모두 togehter에 넣을 수 없다. 어떤 도움을 주셔서 감사합니다.webapi를 통한 AngularJS 클라이언트 측 라우팅 및 토큰 인증
질문 :
- 가장 좋은 방법은 무엇입니다 : 쿠키 또는 토큰 기반?
- 각 요청마다 인증을 위해 무기 토큰을 생성하려면 어떻게해야합니까?
- API 함수에 대한 유효성 확인?
- 클라이언트에서 서명 된 인증을 어떻게 보존합니까?
예제 코드 :
<form name="form" novalidate>
<input type="text" ng-model="user.userName" />
<input type="password" ng-model="user.password" />
<input type="submit" value="Sign In" data-ng-click="signin(user)">
</form>
인증 각도 컨트롤러
$scope.signin = function (user) {
$http.post(uri + 'account/signin', user)
.success(function (data, status, headers, config) {
user.authenticated = true;
$rootScope.user = user;
$location.path('/');
})
.error(function (data, status, headers, config) {
alert(JSON.stringify(data));
user.authenticated = false;
$rootScope.user = {};
});
};
내 API 백엔드 API 코드 형태로
로그인. 컨텐츠를 제한하기위한 JWT 라이브러리를 사용
[HttpPost] public HttpResponseMessage SignIn(UserDataModel user) { //FormsAuthetication is just an example. Can I use OWIN Context to create a session and cookies or should I just use tokens for authentication on each request? How do I preserve the autentication signed in user on the client? if (this.ModelState.IsValid) { if (true) //perform authentication against db etc. { var response = this.Request.CreateResponse(HttpStatusCode.Created, true); FormsAuthentication.SetAuthCookie(user.UserName, false); return response; } return this.Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Invalid username or password"); } return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState); }
인증 .
config.MessageHandlers.Add(new JsonWebTokenValidationHandler { Audience = "123", SymmetricKey = "456" });
내 API 방법
쿠키 인증 또는 (무기명)를 사용할지 여부[Authorize] public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; }
정말 대단한 설명입니다. 제가 찾고 있었던 것입니다. 답변과 자세한 예제를 보내 주셔서 감사합니다. –
물론 도움이 되었으면 좋겠다. – bernhardw
숨겨진 i- 프레임 (인증 서버가 이전 사용 권한을 기억하고 있다고 가정)에 의해 액세스 토큰을 새로 고치는 방법이 있습니까? – g18c