1. 程式人生 > >wcf 登錄認證 angular 認證重定向

wcf 登錄認證 angular 認證重定向

管理 ppr nco ole cti via 管理器 prot .when

自定義認證管理器:

 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‘);

wcf 登錄認證 angular 認證重定向