asp.net中缺少FindByEmail

时间:2016-04-05 07:05:30

标签: c# asp.net entity-framework asp.net-identity

如何在ASP.net中启用方法' FindByEmail'?

我在asp.net中有以下代码

档案RoleActions.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
...
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

...
            IdentityResult IdRoleResult;
            IdentityResult IdUserResult;

            //Create a Role object by using the ApplicationDbContext object.
            // The RoleStore is only allowed to contain IdentityRole objects.
            var roleStore = new RoleStore<IdentityRole>(context);

            //Create a RoleManager object that is only allowed to contain IdentityRole objects.
            //When creating the RoleManager object, you pass in (as a parameter) a new RoleStore object.
            var roleMgr = new RoleManager<IdentityRole>(roleStore);

            //Then, you create the "canEdit" role if it doesn't already exist.
            if (!roleMgr.RoleExists("canEdit"))
            {
                IdRoleResult = roleMgr.Create(new IdentityRole { Name = "canEdit" });
            }

            // Create a UserManager object based on the UserStore object and the ApplicationDbContext object. Note that you can create new objects and use the as parameters in a single line of code, rather than using multiple lines of code, as you did for the RoleManager object.

            var userMgr = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var appUser = new ApplicationUser
            {
                UserName = "canEditUser@example.com",
                Email = "canEditUser@example.com"
            };
            IdUserResult = userMgr.Create(appUser, "password1");

            // If the new "canEdit" user was successfully created, add the "canEdit" user to the "canEdit" role.
            if (!userMgr.IsInRole(userMgr.FindByEmail("canEditUser@example.com").Id, "canEdit"))
            {
                IdUserResult = userMgr.AddToRole(userMgr.FindByEmail("canEditUser@example.com").Id, "canEdit");
            }
        }
    }
}

文件IdentityModels.cs

// You can add User data for the user by adding more properties to your User class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{       
}

我在构建时遇到错误:

  

&#34;错误1&#39; Microsoft.AspNet.Identity.UserManager&lt; .... ApplicationUser&gt;&#39;   不包含&#39; FindByEmail&#39;的定义没有延伸   方法&#39; FindByEmail&#39;接受第一个类型的参数   &#39;&Microsoft.AspNet.Identity.UserManager LT; .... ApplicationUser&GT;&#39;可能   发现(你错过了使用指令或程序集   参考)...&#34;

1 个答案:

答案 0 :(得分:0)

ASP.NET Identity 2.0 中添加了FindByEmail(请参阅声明https://blogs.msdn.microsoft.com/webdev/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1/)。

让FindByEmail安装最新的软件包

  • Microsoft.AspNet.Identity.EntityFramework

  • Microsoft.AspNet.Identity.Core

  • Microsoft.AspNet.Identity.OWIN