asp.net-mvc - 我的 MVC 5 模板中没有 ApplicationRoleManager 类

标签 asp.net-mvc owin

当我尝试将 app.CreatePerOwinContext(ApplicationRoleManager.Create) 添加到 configauth 时,我被告知 applicationRoleManager 不存在。有谁知道为什么吗?

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);

最佳答案

我遇到了同样的问题,并遇到了这段代码,我用它来将类添加到我的项目中:

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 中添加这段代码来配置它:

app.CreatePerOwinContext<ApplicationRoleManager>(Application‌​RoleManager.Create); 

完整文章可在此处获取:http://www.codeproject.com/Articles/762428/ASP-NET-MVC-and-Identity-Understanding-the-Basics

关于asp.net-mvc - 我的 MVC 5 模板中没有 ApplicationRoleManager 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417024/

相关文章:

asp.net - 在 .NET 标准中创建 OWIN 中间件

asp.net - F# 中的 SignalR 和 SelfHost

c# - 在弹出窗口中使用 Owin 外部登录的 Asp.net 登录提供程序

asp.net - 在 Silverlight HttpWebRequest 中禁用缓存

asp.net-mvc - asp.net mvc 用户负载测试

jquery - 使用 jquery ajax 传递数据

asp.net-mvc - 带有源代码的真实世界 ASP.NET MVC 应用程序?

c# - 如何在自托管 Web API 上发送文件并在服务器上处理它?

asp.net - 安装 ASP.NET MVC 4 Beta 后引发 InvalidCastException

c# - 修改 OWIN OAuth 中间件以使用 JWT 不记名 token