私はMVCを初めて使用しています。私はロールマネージャの作成に取り組んでいます。今のところ、私はweb.configのadminユーザーの資格情報を追加しました。エンティティタイプIdentityRoleは現在のコンテキストのモデルの一部ではありません。Asp.net mvc
データ型の文字列を整数に変更しようとしましたが、必要な場所にコードを追加して更新しました。現時点では、プロジェクトを構築する際にエラーは発生しません。私は私のウェブサイトを実行すると、私は上記のエラーが発生します。 追加情報:エンティティタイプIdentityRoleは、現在のコンテキストのモデルの一部ではありません。
ここは私のコードです。
private void CreateAdminIfNeeded()
{
// Get Admin Account
string AdminUserName = ConfigurationManager.AppSettings["AdminUserName"];
string AdminPassword = ConfigurationManager.AppSettings["AdminPassword"];
string fileName = HttpContext.Server.MapPath(@"~/Images/noImg.png");
byte[] imageData = null;
FileInfo fileInfo = new FileInfo(fileName);
long imageFileLength = fileInfo.Length;
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
imageData = br.ReadBytes((int)imageFileLength);
// See if Admin exists
var objAdminUser = UserManager.FindByEmail(AdminUserName);
if (objAdminUser == null)
{
//See if the Admin role exists. In this part I am getting error
if (!RoleManager.RoleExists("Administrator"))
{
// Create the Admin Role (if needed)
IdentityRole objAdminRole = new IdentityRole("Administrator");
RoleManager.Create(objAdminRole);
}
// Create Admin user
var objNewAdminUser = new ApplicationUser { UserName = AdminUserName, Email = AdminUserName, UserPhoto = imageData };
var AdminUserCreateResult = UserManager.Create(objNewAdminUser, AdminPassword);
// Put user in Admin role
UserManager.AddToRole(objNewAdminUser.Id, "Administrator");
}
}
#endregion
ここではエラーが発生しています。
//See if the Admin role exists.
if (!RoleManager.RoleExists("Administrator"))
{
// Create the Admin Role (if needed)
IdentityRole objAdminRole = new
アイデンティティモデルコード:
名前空間SoftechGoSMS.Models { パブリッククラスApplicationUser:IdentityUser { 公共バイト[] UserPhoto {取得します。セット; }
public string DomainName { get; set; }
public string CompanyName { get; set; }
public string CopyrightInformation { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }
public class CustomRole : IdentityRole<int, CustomUserRole>
{
public CustomRole() { }
public CustomRole(string name) { Name = name; }
}
public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public CustomUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
public CustomRoleStore(ApplicationDbContext context)
: base(context)
{
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public ApplicationDbContext()
: base("SMSGoConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
#region public ApplicationRoleManager RoleManager
public ApplicationRoleManager RoleManager
{
get
{
return _roleManager ??
HttpContext.GetOwinContext()
.GetUserManager<ApplicationRoleManager>();
}
private set
{
_roleManager = value;
}
}
アプリケーションロールマネージャコード:
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole, string> store)
: base(store)
{
}
public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
{
var roleStore = new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>());
return new ApplicationRoleManager(roleStore);
}
}
スタートアップ認証コード:
あなたが文字列からIDを作成するカスタムロールを実装public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Add Role Manager
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: id => id.GetUserId<int>())
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));
app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);
}
}
のようなあなたのApplicationRoleManagerを変更
新しい役割を作成するomRoleはあなたのアイデンティティのモデルを表示します。 –
@ UmairAnwaar IDモデルで最新の質問を確認してください。 – user7090664
CustomRole()を使用してIdentityRoleではなくロールを作成します。 –