Ibrahim Shaikh
Ibrahim Shaikh

Reputation: 398

Owin Based authentication - No 'Configuration' method was found in web api

I never worked on authentication before and want to learn authentications in asp.net web api's

I created a project, and start working with the help of this Tutorial

But I am getting the below error

The following errors occurred while attempting to load the app. - No 'Configuration' method was found in class 'OwinBasedToken.Startup, OwinBasedToken, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.EntryPointNotFoundException: The following errors occurred while attempting to load the app. - No 'Configuration' method was found in class 'OwinBasedToken.Startup, OwinBasedToken, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config. To specify the OWIN startup Assembly, Class, or Method, add the appSetting owin:AppStartup with the fully qualified startup class or configuration method name in your web.config.

here is my code

WebApiConfig.cs

using System.Web.Http;

namespace OwinBasedToken
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

Startup.cs

using Microsoft.Owin;
using Microsoft.Owin.Security.OAuth;
using Owin;
using OwinBasedToken.Provider;
using System;
using System.Web.Http;

[assembly: OwinStartup(typeof(OwinBasedToken.Startup))]

namespace OwinBasedToken
{
    public class Startup
    {
        public void configuration(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();
            configureOAuth(app);

            WebApiConfig.Register(config);
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            app.UseWebApi(config);
        }

        public void configureOAuth(IAppBuilder app)
        {
            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp = true,
                TokenEndpointPath = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(60),
                Provider = new SimpleAuthorizationServerProvider()
            };
            app.UseOAuthAuthorizationServer(OAuthServerOptions);
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
        }
    }
}

SimpleAuthorizationServerProvider.cs

using Microsoft.Owin.Security.OAuth;
using System.Threading.Tasks;
using System.Security.Claims;

namespace OwinBasedToken.Provider
{
    public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {
        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            context.Validated();
        }

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

OwinTokenController

using System.Web.Http;

namespace OwinBasedToken.Controllers
{
    public class OwinTokenController : ApiController
    {
        [Authorize]
        public IHttpActionResult Authorize()
        {
            return Ok("Authorized");
        }
    }
}

I just want to know, what I am doing wrong here?

Did I missed something?

enter image description here

Upvotes: 0

Views: 2184

Answers (2)

Tan Sang
Tan Sang

Reputation: 2069

Change your coding convention. In C# we are using "PascalCase". Fix your configuration and configurationOauth method to Configuration and ConfigurationOauth(optional) for work. Good luck.

P/s: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions hope this help to u

Upvotes: 5

Lajos Arpad
Lajos Arpad

Reputation: 76414

You will need to define Configuration, not configuration:

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();
        configureOAuth(app);

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
    }

Upvotes: 1

Related Questions