简体   繁体   中英

Angular.js SPA security with ASP.NET MVC and WebApi

I'm building a SPA using Angular.js and ASP.NET and I would like to know what is the best way to secure it.

Here is what I need :

I would like to use MVC framework to hide my application only to logged users. So the first thing that users will do before launching the SPA will be to log into the website using a simple login form.

When the Angular app will be launched, it will communicate with my ApiController using REST requests.

I also want my user to be logged out automatically after 20 minutes of inactivity.

I know that REST is supposed to be stateless... but I can't figure how to implement all I need without sessions...

But on the other side, I want to be able to use my WebAPI with a future mobile application. I will have to use Tokens for the authentication on this application.

What is the best way for me to achieve that kind of authentication?

Thanks for your time!

I developed an entire security layer with the same conditions as yours following those very well explained in this post here .

BTW, the token will expire automatically after 20 minutes because when you create it you will set it's expiration date immediately; every time you're going to make a request, the system will check the token exp date with the current date, refusing your token if the time passed. For example this a tipical oauth server configuration with token and refresh token settings:

internal static OAuthAuthorizationServerOptions GetAuthorizationServerOptions(IComponentContext scope)
    {
        OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions
        {                
            AllowInsecureHttp = true,
            ApplicationCanDisplayErrors = true,
            TokenEndpointPath = new PathString(Constants.PublicAuth.OAUTH_TOKEN_PATH),
            AuthorizeEndpointPath = new PathString(Constants.ExternalAuth.AUTH_ENDPOINT),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(Constants.PublicAuth.TOKEN_EXPIRATION_IN_MINUTES),
            Provider = scope.Resolve<AuthorizationServerProvider>(),
            AccessTokenFormat = new CustomJwtFormat(),
            RefreshTokenProvider = scope.Resolve<SimpleRefreshTokenProvider>()
        };
        return oAuthServerOptions;
    }

The refresh token is also very useful, but you have to manage the token replacement by yourself; for example in our application we pass every API call through a single service that, if the server responds 401 (unauthorized) , it will try to request a new token using the refresh token and then it will try the same call again. Only after the second failure you'll be redirected to the login page.

For example:

function executeCallWithAuth(method, url, payload, params) {
    var defer = $q.defer();
    debug.logf('{0}: {1}', method.toUpperCase(), url);
    $http({ method: method, url: url, data: payload, headers: createHeaders(), params: params }).then(
        function(results) { defer.resolve(results); }, 
        function(error) {
            if (error.status !== 401) defer.reject(error);
            else {
                debug.warn(`Call to: ${method}:${url} result in 401, try token refresh...`);
                auth.refreshToken().then(
                    function() {
                        debug.warn('Token refresh succesfully, retry api call...');
                        $http({ method: method, url: url, data: payload, headers: createHeaders() }).then(
                            function(results) { defer.resolve(results); },
                            function(errors) { defer.reject(errors); });
                    },
                    function(tokenError) {
                        debug.warn('Token refresh rejected, redirect to login.');
                        $state.go('login');
                        defer.reject(tokenError);
                    });
            }
        });

    return defer.promise;
}

and

    function createHeaders() {
    var headers = {
    };

    var authData = storage.get('authorizationData');
    if (authData) {
        headers.Authorization = 'Bearer ' + authData.token;
    }

    return headers;
}

Using Angular the best way to secure a route is "do not create a route". Basically, you need to load the user profile, and only after that you will create the routes only to the pages he can navigate to. If you don't create the route for a page you don't need to secure that page: Angular will automatically send the user to a 404.

I would secure your WebAPI calls with OAuth2 (you can even use the built in Identity 2.0 provider that comes baked in with it). Keep your WebAPI stateless, use SSL (consider a filter to force it), and use the [Authorize] tags to secure you services. On the MVC side, this will have to maintain state and you will want to have the login form get an OAuth2 token from your WebAPI layer and pass that down into Angular. Set the expiration on this to 20 minutes. You can also use the cookies authentication model here since it will need to be stateful on the MVC side, but all ajax calls made to the WebAPI layer by Angular will need to pass the OAuth2 token as a bearer token in the Authorization request header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM