IdentityConfig.cs is missing in my C# web applicat

2020-06-23 07:34发布

问题:

I followed the steps from below link so that I could add Email verification to my project:

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

I am wondering where is IdentityConfig.cs and I can't find this file in my project.

Instead of IdentityConfig.cs, in which file can I insert my code?

回答1:

Oh my god I have the same problem, so I'm going to use my older project for 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);
    }
}

Hope it helps you, I'm not sure But I think in Visual studio 2013 in Update ver.2 we don't have IdentityConfig.cs.



回答2:

Install or Upgrade 'Microsoft.AspNet.Identity.EntityFramework' using the 'Manage NuGet Packages...'



回答3:

Microsoft apparently removed IdentityConfig.cs file for some updates of Visual Studio 2013 thinking that would make things easier. However, since Update 4 they have added it back. Moreover, Identity has seen many changes over the years. Solutions that used to work for 2.0 and 2.1 no longer works for current version.

Here is what you can do: You need to update your Visual Studio 2013 to Update 4 or above. Right now I think the latest update is Update 5.

Once you do that you will find that it automatically adds the IdentityConfig.cs file in your project(MVC template).



回答4:

This happens when you create a empty project. When you create a MVC project, that means when you select mvc tick box as you create the new project it will generate all the templates including the identity framework. So what you guys have to do is create a new MVC project and copy those files related to identity framework in to your project :)



回答5:

Before creating the project, make sure to have "Individual User Accounts" selected under "Authentication"