AngularJS + ASP.NET Web API + ASP.NET MVC Authentication

前端 未结 3 1743
有刺的猬
有刺的猬 2020-12-22 18:37

I am new to AngularJS and trying to evaluate it for my new web application.

Requirement:

I will have one ASP.NET Web API which will be consumed from an And

3条回答
  •  一整个雨季
    2020-12-22 19:10

    I solved the problem with a very strait forward solution. I just made sure I have following two lines of code in the Register method of WebApiConfig:

    config.SuppressDefaultHostAuthentication();
    config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
    

    That's it. Now my MVC controllers look for the session cookie for authorization whereas my Web API controllers look for the auth token in the header of each request. Moreover, the Web API sends a token (JSON) and a session cookie itself in response to the login/authentication request e.g. http:\\www.mydomain.com\token.

    So now I send my login request to Web API to get the token as well as the session cookie. Session cookie will automatically be sent will each request so I don't have to worry about the authorization of my MVC controllers. For Web API calls I am sending the auth token in the header for each request as Web API controllers don't care about the session cookie being sent with the request.

提交回复
热议问题