AngularJS routing po stronie klienta i uwierzytelnianie tokena za pomocą webapi
Chcę utworzyć przykład uwierzytelniania i autoryzacji w aplikacji SPA angularjs przy użyciu asp.net mvc webapi jako routingu backend i po stronie klienta (bez cshtml). Poniżej znajduje się tylko przykład funkcji, których można użyć do skonfigurowania pełnego przykładu. Ale nie mogę tego pojąć. Każda pomoc doceniana.
Pytania:
Jaka jest najlepsza praktyka: Cookie lub Token based?Jak utworzyć token okaziciela w kanciasty, aby autoryzować na każde żądanie?Sprawdzanie poprawności funkcji API?Jak zachować autentykację podpisaną przez użytkownika na kliencie?Przykładowy kod:
Zaloguj się w formularzu
<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>
Kontroler kątowy uwierzytelniania
$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 = {};
});
};
Kod interfejsu API mojego interfejsu API.
[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);
}
Autoryzacja Używanie biblioteki JWT do ograniczania zawartości.
config.MessageHandlers.Add(new JsonWebTokenValidationHandler
{
Audience = "123",
SymmetricKey = "456"
});
Moje metody API
[Authorize]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}