基于 Owin 的身份验证 - 在 Web api 中未找到 'Configuration' 方法

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

我以前从未从事过身份验证工作,想在 asp.net web api 的

中学习身份验证

我创建了一个项目,并在 this 教程

的帮助下开始工作

但我收到以下错误

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.

这是我的代码

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

我只想知道,我做错了什么?

我是不是漏掉了什么?

您需要定义 Configuration,而不是 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);
    }

更改您的编码约定。 在 C# 中,我们使用 "PascalCase"。将您的配置和 configurationOauth 方法修复为 Configuration 和 ConfigurationOauth(optional) 以供工作。祝你好运。

P/s:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions希望对你有所帮助