简体   繁体   English

找不到类型或命名空间名称'SignInManager'(您是否缺少using指令或程序集引用?)

[英]The type or namespace name 'SignInManager' could not be found (are you missing a using directive or an assembly reference?)

I've been following the steps given in this website: http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/checkout-and-payment-with-paypal . 我一直在关注本网站提供的步骤: http//www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/checkout-and- pay-with-paypal I am already on the "Modifying Login Functionality". 我已经在“修改登录功能”。 There seems to be a problem in the SignInManager.When I hover the pointer, it appears " The type or namespace name 'SignInManager' could not be found (are you missing a using directive or an assembly reference?) ". SignInManager中似乎存在问题。当我将鼠标悬停在指针上时,会出现“ 找不到类型或命名空间名称'SignInManager'(您是否缺少using指令或程序集引用?) ”。 Can you help found out why? 你能帮忙找出原因吗?

using System;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using WebApplication1.Models;

namespace WebApplication1.Account
{
    public partial class Login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register";
            // Enable this once you have account confirmation enabled for password reset functionality
            //ForgotPasswordHyperLink.NavigateUrl = "Forgot";
            OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
            var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            if (!String.IsNullOrEmpty(returnUrl))
            {
                RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
            }
        }

        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                // Validate the user password
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();

                // This doen't count login failures towards account lockout
                // To enable password failures to trigger lockout, change to shouldLockout: true
                var result = **signinManager**.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: true);

                switch (result)
                {
                    case SignInStatus.Success:
                        WingtipToys.Logic.ShoppingCartActions usersShoppingCart = new WingtipToys.Logic.ShoppingCartActions();
                        String cartId = usersShoppingCart.GetCartId();
                        usersShoppingCart.MigrateCart(cartId, Email.Text);

                        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                        break;
                    case SignInStatus.LockedOut:
                        Response.Redirect("/Account/Lockout");
                        break;
                    case SignInStatus.RequiresVerification:
                        Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
                                                        Request.QueryString["ReturnUrl"],
                                                        RememberMe.Checked),
                                          true);
                        break;
                    case SignInStatus.Failure:
                    default:
                        FailureText.Text = "Invalid login attempt";
                        ErrorMessage.Visible = true;
                        break;
                }
            }
        }
    }
}   

When I check the IdentityConfig.cs, it also shows an error. 当我检查IdentityConfig.cs时,它也显示错误。 Not only one but three. 不仅是一个而且是三个。 The following lines are: 以下行是:

SignInManager base(userManager, authenticationManager) UserManager SignInManager base(userManager,authenticationManager)UserManager

The code is below. 代码如下。 The lines above are located at the bottom: 上面的行位于底部:

using System;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using WebApplication1.Models;

namespace WebApplication1
{
    public class EmailService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your email service here to send an email.
            return Task.FromResult(0);
        }
    }

    public class SmsService : IIdentityMessageService
    {
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your SMS service here to send a text message.
            return Task.FromResult(0);
        }
    }

    // Configure the application user manager used in this application. UserManager is defined in ASP.NET Identity and is used by the application.
    public class ApplicationUserManager : UserManager<ApplicationUser>
    {
        public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
        }

        public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
        {
            var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = false,
                RequireUniqueEmail = true
            };

            // Configure validation logic for passwords
            manager.PasswordValidator = new PasswordValidator
            {
                RequiredLength = 6,
                RequireNonLetterOrDigit = true,
                RequireDigit = true,
                RequireLowercase = true,
                RequireUppercase = true,
            };

            // Register two factor authentication providers. This application uses Phone and Emails as a step of receiving a code for verifying the user
            // You can write your own provider and plug it in here.
            manager.RegisterTwoFactorProvider("Phone Code", new PhoneNumberTokenProvider<ApplicationUser>
            {
                MessageFormat = "Your security code is {0}"
            });
            manager.RegisterTwoFactorProvider("Email Code", new EmailTokenProvider<ApplicationUser>
            {
                Subject = "Security Code",
                BodyFormat = "Your security code is {0}"
            });

            // Configure user lockout defaults
            manager.UserLockoutEnabledByDefault = true;
            manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
            manager.MaxFailedAccessAttemptsBeforeLockout = 5;

            manager.EmailService = new EmailService();
            manager.SmsService = new SmsService();
            var dataProtectionProvider = options.DataProtectionProvider;
            if (dataProtectionProvider != null)
            {
                manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
            }
            return manager;
        }
    }

    public class ApplicationSignInManager : **SignInManager<ApplicationUser, string>**

    {
        public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager) :
            **base(userManager, authenticationManager)** { }

        public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
        {
            return user.GenerateUserIdentityAsync((ApplicationUserManager)**UserManager**);
        }

        public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
        {
            return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        }
    }
}

So much time passed but I had same problem and I found solution just five minutes ago. 这么多时间过去但我有同样的问题,我在五分钟前找到了解决方案。

First: check your Microsoft.AspNet.Identity.Owin version. 首先:检查您的Microsoft.AspNet.Identity.Owin版本。 You should have minimum 2.1.0 (but better install newset - now it's 2.2.1). 你应该有最低2.1.0(但更好的安装newset - 现在它是2.2.1)。

If you have to updated - install it with NuGet: 如果必须更新 - 使用NuGet安装它:

Update-Package Microsoft.AspNet.Identity.Owin -Version 2.2.1

After add below class to App_Start/IdentityConfig.cs: 将以下类添加到App_Start / IdentityConfig.cs后:

public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
    public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
        : base(userManager, authenticationManager)
    {
    }

    public override Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
    {
        return user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
    }

    public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
    {
        return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
    }
}

below line to App_Start/Startup.Auth.cs: 下面一行到App_Start / Startup.Auth.cs:

 app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

and below class to AccountController (just under constructors): 以及类下面的AccountController(在构造函数下):

    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set
        {
            _signInManager = value;
        }
    }

and the last one - modify constructor: 和最后一个 - 修改构造函数:

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到类型或名称空间名称(缺少使用指令或程序集引用吗?) - Type or namespace name could not be found (missing using directive or assembly reference?) 找不到类型或名称空间名称“ My_Interface_Name”(您是否缺少using指令或程序集引用?) - The type or namespace name 'My_Interface_Name' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ FacebookSessionClient”(您是否缺少using指令或程序集引用?) - The type or namespace name 'FacebookSessionClient' could not be found (are you missing a using directive or an assembly reference?) 错误 CS0246:找不到类型或命名空间名称“IWebHostEnvironment”(您是否缺少 using 指令或程序集引用?) - error CS0246: The type or namespace name 'IWebHostEnvironment' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ PrintDocument”(是否缺少using指令或程序集引用?) - The type or namespace name 'PrintDocument' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或命名空间名称“OleDbConnection”(您是否缺少 using 指令或程序集引用?) - The type or namespace name 'OleDbConnection' could not be found (are you missing a using directive or an assembly reference?) 为什么会出现这个错误? “找不到类型或名称空间名称&#39;c&#39;(您是否缺少using指令或程序集引用?)” - Why this error ? “The type or namespace name 'c' could not be found (are you missing a using directive or an assembly reference?)” 找不到类型或命名空间名称&#39;ContractClassFor&#39;(您是否缺少using指令或程序集引用?) - The type or namespace name 'ContractClassFor' could not be found (are you missing a using directive or an assembly reference?) 错误1找不到类型或名称空间名称&#39;FPSTimer&#39;(您是否缺少using指令或程序集引用?) - Error 1 The type or namespace name 'FPSTimer' could not be found (are you missing a using directive or an assembly reference?) 找不到类型或名称空间名称“ HttpConfiguration”(是否缺少using指令或程序集引用? - The type or namespace name 'HttpConfiguration' could not be found (are you missing a using directive or an assembly reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM