wcf 登录认证 angular 认证重定向

自定义认证管理器:

 1     class WmsServiceAuthorizationManager: ServiceAuthorizationManager
 2     {
 3         protected override bool CheckAccessCore(OperationContext operationContext)
 4         {
 5             var via = operationContext.IncomingMessageProperties.Via;
 6             var ctx = WebOperationContext.Current;
 7             var token = ctx.IncomingRequest.Headers["token"];
 8             if (via.Segments.Count(x=>x.Equals("login", StringComparison.OrdinalIgnoreCase)) == 0 
 9                 && token != "1234")
10             {
11                 ctx.OutgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
12                 return false;
13             }
14             return true;
15         }
16     }

宿主绑定认证管理器:

1         static void Main(string[] args)
2         {
3             ServiceHost host = new ServiceHost(typeof(Wms.Service.WmsService));
4             host.Authorization.ServiceAuthorizationManager = new WmsServiceAuthorizationManager();
5             host.Open();
6             Console.WriteLine("已启动");
7             Console.WriteLine("回车键退出");
8             Console.ReadLine();
9         }

angular请求前统一添加令牌,和认证失败后重定向:

 1     var app = angular.module('App', ['ui.router']);
 2 
 3     app.factory('interceptor', function ($q, $location) {
 4         return {
 5             request: function (config) {
 6                 //console.log(config.url);
 7                 if (config.url.indexOf('/login/') === -1) {
 8                     config.headers['token'] = '1234';
 9                 }
10                 //console.log(config.headers);
11                 return config || $q.when(config);
12             },
13             response: function (response) {
14                 if (response.config.url.indexOf('service') > -1) {
15                     //todo 预处理请求结果
16                 }
17                 return response || $q.when(response);
18             },
19             responseError: function (response) {
20                 //console.log(response.status + ' ' + response.statusText);
21                 if (response.status === 401) {// If our response status is unauthorized
22                     $location.path('/main/index');// Redirect to the login screen
23                 } else {
24                     return $q.reject(response);// Reject our response
25                 }
26             }
27         };
28     });
1 $httpProvider.interceptors.push('interceptor');

猜你喜欢

转载自www.cnblogs.com/jonney-wang/p/9426752.html