"ConfigureAuth"这个名称在当前上下文中不存在。

10

我尝试运行我的页面时出现错误提示:

当前上下文中不存在名为 'ConfigureAuth' 的名称

我的 Stratup 类中存在这个问题。我确认所有 AspNet Identity 库都已安装。接下来我需要做什么来尝试解决这个问题?

using Microsoft.Owin;
using Owin;
[assembly: OwinStartupAttribute(typeof(project_name.Startup))]
namespace project_name
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
     }
}

你在哪里遇到了错误?是在构建时,运行在IIS中,运行在IIS Express中,运行在Cassini中,还是通过mod-mono在Linux上运行...? - Ryan Mann
6个回答

9
如果您使用默认的Visual Studio项目模板,则ConfigureAuth方法可以在部分类Startup.Auth.cs中找到。因此,在修改项目结构时,请确保没有破坏任何内容。
这是一个ConfigureAuth方法的示例:
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context and user manager to use a single instance per request
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

    // Enable the application to use a cookie to store information for the signed in user
    // and to use a cookie to temporarily store information about a user logging in with a third party login provider
    app.UseCookieAuthentication(new CookieAuthenticationOptions());
    app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

    // Configure the application for OAuth based flow
    PublicClientId = "self";
    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/api/Token"),
        Provider = new ApplicationOAuthProvider(PublicClientId),
        AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
        AllowInsecureHttp = true
    };

    // Enable the application to use bearer tokens to authenticate users
    app.UseOAuthBearerTokens(OAuthOptions);
}

5
对我来说,这是一个命名空间问题,请查看this,希望能帮助到某些人。 - Shaiju T
我遇到了同样的问题。我使用了using语句来解决命名空间的差异,但是没有成功。我不理解的是,如果两个部分Startup类的命名空间都是在使用模板时创建的,那么为什么会出现错误?它使用解决方案的名称作为Startup.cs部分的名称,使用项目的名称作为Startup.Auth.cs部分的名称。 - Edward
你能列出这里使用了哪些命名空间吗?我找不到 ApplicationUserManagerOAuthServerOptions 等等。 - Scott Baker

8

我遇到了类似的问题,为了解决它,我在 Startup.Auth.cs 文件中的命名空间中删除了 .App_Start。之后,我成功看到了引用。


3

这可能是:

    [assembly: **OwinStartup**(typeof(Project-Name.Startup))]
    namespace project-name
    {
        public partial class Startup
        {
            public void **Configuration**(IAppBuilder app)
                    {

或者

    [assembly: **OwinStartupAttribute**(typeof(Project-Name.Startup))]
    namespace project-name
    {
        public partial class Startup
        {
            public void **ConfigureAuth**(IAppBuilder app)
                    {

请将OwinStartupAttribute重命名为OwinStartup,或将Configuration重命名为ConfigureAuth。


1
请注意,两个部分类(Startup.Auth.csStartup.cs)应该在同一个命名空间中,该命名空间是项目的根目录,只需将 Startup.Auth.cs 的命名空间更改为 Startup.cs 相同的命名空间即可。

0

命名空间 PAYOnline.App_Start

删除 App_Start 命名空间,仅保留 PAYOnline => 做得好


0

在创建项目时,请确保名称中没有空格。 例如,我的应用程序名为“DevOps Test”,在运行时会出现错误。 我将其重新命名为“DevopsTest”,问题就解决了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接