1

I have ASP.NET MVC project and using Form Authentication (Cookie base) with ASP.NET Identity. I added WEB API controller into that project. Now What I want is for MVC project it should use Form Authentication using Cookie but for API it should use token base, How I can configure it.

Regards, Imran Ahmad

1 Answer 1

2

Firstly install these NuGet packages,

1.Install-Package Microsoft.AspNet.WebApi.Owin

2.Install-Package Microsoft.Owin.Host.SystemWeb

3.Install-Package Microsoft.Owin.Security.OAuth

then, the project must be having a StartUp.cs file.
Add this code to your file

    OAuthAuthorizationServerOptions OAuthOptions = new OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/api/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(2),                
        Provider = new AuthorizationServerProvider()
    };

    // To Generate token
    app.UseOAuthAuthorizationServer(OAuthOptions);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

Add this code to new AuthorizationServerProvider.cs file

public class AuthorizationServerProvider: OAuthAuthorizationServerProvider
{
     public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
     {
         context.Validated();
     }

     public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
     {
         var identity = new ClaimsIdentity(context.Options.AuthenticationType);
         context.Validated(identity);
     }
}

You can test the code using Postman API client by posting to your endpoint

yourwebsite/api/token 

along with grant_type, username and password in the
x-www-form-Urlencoded tab of the postman.

The response of which you will get an access_token, put this access token put it in your header while calling your Resource controller.

For further references refer http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/.

Sign up to request clarification or add additional context in comments.

3 Comments

I think is for token base authentication, but I want that both authentication e.g Form Authentication using Cookie and Token base authentication should exist. What about the Form Authentication in that case?
ok, you mean to say you want to call API controller using either a cookie or using token authentication.
if you are planning to use both the authentication then the MVC cookie will work with the API controller just you have to decorate API controller with Authorize attribute. Add this in Startup.cs - app.UseCookieAuthentication(new CookieAuthenticationOptions());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.