问题描述
当我尝试将app.CreatePerOwinContext(ApplicationRoleManager.Create)添加到configauth时,我被告知applicationRoleManager不存在.有人知道为什么吗?
When I try to add app.CreatePerOwinContext(ApplicationRoleManager.Create) to configauth I am told applicationRoleManager does not exist. Does anybody know why?
using Microsoft.Owin.Security.OAuth;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataProtection;
using Microsoft.Owin.Security.Google;
using Owin;
using System;
using LaCeibaNetv4.Models;
namespace LaCeibaNetv4
{
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 and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
推荐答案
我遇到了同样的问题,并遇到了我用来将类添加到我的项目中的这段代码:
I was having the same issue and came across this code which I used to add the class to my project:
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole,string> roleStore)
: base(roleStore) { }
public static ApplicationRoleManager Create(
IdentityFactoryOptions<ApplicationRoleManager> options,
IOwinContext context)
{
var manager = new ApplicationRoleManager(
new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
return manager;
}
}
此外,请记住在启动时通过在Startup.Auth.cs中添加以下代码来对其进行配置:
Also, remember to have it configured at startup by adding this piece of code in Startup.Auth.cs:
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
可在此处找到完整的文章: http://www.codeproject.com/Articles/762428/ASP-NET-MVC-and-Identity-Understanding-the-Basics
The full article is available here: http://www.codeproject.com/Articles/762428/ASP-NET-MVC-and-Identity-Understanding-the-Basics
这篇关于我的MVC 5模板中没有ApplicationRoleManager类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!