0
votes

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

2
nobody ever reads exception messages :( " No 'Configuration' method was found in class 'OwinBasedToken.Startup, OwinBasedToken, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'" means that there is no Configuration method found in your Startup class. Literally.vasily.sib
@vasily.sib I do have a method named Configuration in my Startup ClassIbrahim Shaikh
No, you haven'tvasily.sib
You have a method named configuration, but you need a method named Configuration. c != Cvasily.sib
Ohhhh I didn't notice this, anyways thanksIbrahim Shaikh

2 Answers

2
votes

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://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions hope this help to u

1
votes

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);
    }