如何启用方法"FindByEmail"在ASP.net 中
我在asp.net 中有以下代码
文件角色操作.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");
}
}
}
}
文件标识模型.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
{
}
我在构建时得到错误:
"错误1'Microsoft.AspNet.Identity.UserManager<….ApplicationUser>'不包含"FindByEmail"的定义,也没有扩展名方法"FindByEmail"接受类型为的第一个参数'Microsoft.AspNet.Identity.UserManager<。。。。ApplicationUser>'可能是已找到(是否缺少using指令或程序集参考?)。。。"
FindByEmail已添加到ASP.NET Identity 2.0中(请参阅公告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