3

AngularJS とすぐに使用できる WebApi2 トークン認証テンプレート (個々のユーザー アカウント) を使用してサイトを作成しています。www.domain.comsub.domain.comの 2 つのサイトに同時にログインしようとしています。

現在、ユーザーを認証するために角度で次のコードを使用しています。

 $http({
            method: 'POST',
            url: '/Token',
            data: serializedData,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).success(function (data, status, headers, config) {
            $window.sessionStorage.token = data.access_token;
        });

次の後に、すべてのリクエストの認証ヘッダーを追加します。

app.factory('authInterceptor', function ($rootScope, $q, $window) {
        return {
            request: function (config) {
                config.headers = config.headers || {};
                if ($window.sessionStorage.token) {
                    $window.sessionStorage.loggedIn = true;
                    config.headers.Authorization = 'Bearer ' + $window.sessionStorage.token;
                }
                return config;
            }
        };
    });

    app.config(function ($httpProvider) {
        $httpProvider.interceptors.push('authInterceptor');
    });

上記のコードでは、各サイトが個別にログインできますが、セッションストレージは他のウィンドウ/タブ間で保持されないため、ユーザーはサブドメインにログインしません。

この問題に関するこのブログ投稿にはいくつかのコメントがあります (半分下): http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/

ただし、実装するには複雑すぎるようです (そして、ユーザーがリダイレクトされるという望ましくない効果があります)。Cookie と同じように、ドメインを設定するのと同じくらい簡単なことを望んでいました。

app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                CookieDomain = ".domain.com"
            });

現在のシナリオで、Cookie よりもトークン認証を使用する必要があるかどうか疑問に思っています...

4

1 に答える 1