我尝试将所有这些框架(来自标题)设置在一起。到目前为止,我设置了IdentitySever3并设法在我的角度2应用程序中的登录按钮上获得登录屏幕。
成功登录后,我从身份验证服务器收到令牌,现在我想从ASP.Net WebApi(.Net 4.6.2)索取一些数据。
我有一个问题。我应该如何配置此服务器应用程序,它能够从角度应用程序中了解已发送令牌的数据。大多数教程都描述了.Net核心。任何人都可以告诉我一些例子,或者说我错过了什么。
答案 0 :(得分:0)
你问的是一个非常笼统的问题,但是以下指针可能会有所帮助:
查看https://github.com/IdentityServer/IdentityServer3.Samples 您应该编写正常的.Net WebApi,并且您需要进行两项基本更改: 1.在服务器上实现“Startup”类,例如其中一个示例(https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/WebHost%20(Windows%20Auth)/WebHost/Startup.cs)。 2.添加在每个控制器/ Web上添加的[Authentication]属性 你想要的方法。
在角度应用上(示例来自angular1,但应该清楚),您需要在每个请求的Authorization标头中发送令牌:
您在模块声明
之后输入的代码.config(($httpProvider: ng.IHttpProvider) => {
$httpProvider.interceptors.push("authInterceptorService");
})
你做了功能
export function authInterceptorService($q, $rootScope, $location) {
return {
request,
responseError
};
function request(config) {
config.headers = config.headers || {};
if (!_.startsWith(config.url, **URLtoYourApi**)) {
return config;
}
config.headers.authorization = "Bearer " + **access token**;
return config;
}
function responseError(rejection) {
if (rejection.status === 401) {
// do some rejection logic
$location.path("/");
}
return $q.reject(rejection);
}
}
以这种方式对每个请求发送令牌=> 访问令牌(可以存储在本地存储中)。显然标记 ..... 意味着您需要替换某些东西:)