我的C#Web应用程序项目中缺少IdentityConfig.cs

时间:2015-02-19 05:56:25

标签: asp.net email identity verification

我按照下面的步骤链接,以便我可以将电子邮件验证添加到我的项目中:

http://www.asp.net/web-forms/overview/security/create-a-secure-aspnet-web-forms-app-with-user-registration,-email-confirmation-and-password-reset#addRes

我想知道IdentityConfig.cs在哪里,我在项目中找不到这个文件。

而不是IdentityConfig.cs,我可以在哪个文件中插入我的代码?

5 个答案:

答案 0 :(得分:1)

哦,天哪,我有同样的问题,所以我将把我的旧项目用于Identityconfig.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using MyEuAdd.Models;

namespace MyEuAdd
{
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,
        };

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

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

// Configure the application sign-in manager which is used in this application.
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);
    }
}

希望它对您有所帮助,我不确定但我认为在Visual Studio 2013的Update ver.2中我们没有IdentityConfig.cs。

答案 1 :(得分:0)

使用“管理NuGet包...”安装或升级“Microsoft.AspNet.Identity.EntityFramework”

答案 2 :(得分:0)

微软显然删除了IdentityConfig.cs文件以获取Visual Studio 2013的一些更新,这些更新会让事情变得更容易。但是,自Update 4以来,他们已将其添加回来。此外,Identity多年来经历了许多变化。过去适用于2.0和2.1的解决方案不再适用于当前版本。

以下是您可以做的事情: 您需要将Visual Studio 2013更新到Update 4或更高版本。现在我认为最新的更新是Update 5.

一旦你这样做,你会发现它会自动在你的项目中添加IdentityConfig.cs文件(MVC模板)。

答案 3 :(得分:0)

创建空项目时会发生这种情况。当您创建MVC项目时,这意味着当您在创建新项目时选择mvc复选框时,它将生成包括标识框架在内的所有模板。那么你们要做的就是创建一个新的MVC项目并将与身份框架相关的文件复制到你的项目中:)

答案 4 :(得分:0)

在创建项目之前,请确保在“身份验证”下选择“个人用户帐户”