ASP.NET MVC身份验证默认控制器和视图未被生成。
创始人
2024-09-16 16:30:25
0

在ASP.NET MVC中,身份验证默认控制器和视图可以通过以下步骤生成:

  1. 在Visual Studio中创建一个新的ASP.NET MVC项目。

  2. 在项目中打开“Tools”菜单,选择“NuGet包管理器”,然后选择“程序包管理器控制台”。

  3. 在控制台中运行以下命令来安装所需的包:

Install-Package Microsoft.AspNet.Identity.Samples -Version 2.2.1
  1. 运行上述命令后,将自动生成IdentityConfig.cs文件,并且默认控制器和视图将被生成。

以下是生成默认控制器和视图的代码示例:

在Global.asax.cs文件中,添加以下代码:

using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using MvcIdentitySample.Models;

namespace MvcIdentitySample
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            InitializeIdentity();
        }

        private void InitializeIdentity()
        {
            ApplicationDbContext context = new ApplicationDbContext();
            IdentityManager manager = new IdentityManager(new UserManager(new UserStore(context)));

            manager.CreateRole("Admin");
            manager.CreateRole("User");

            manager.AddUserToRole("admin@example.com", "Admin");
            manager.AddUserToRole("user@example.com", "User");
        }
    }
}

在IdentityConfig.cs文件中,添加以下代码:

using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;
using MvcIdentitySample.Models;

namespace MvcIdentitySample
{
    public class IdentityConfig
    {
        public void Configuration(IAppBuilder app)
        {
            app.CreatePerOwinContext(() => new ApplicationDbContext());
            app.CreatePerOwinContext>((options, context) =>
                new UserManager(new UserStore(context.Get())));
            app.CreatePerOwinContext>((options, context) =>
                new RoleManager(new RoleStore(context.Get())));

            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });
        }
    }
}

运行应用程序后,将会生成默认的Account控制器和视图。

相关内容

热门资讯

wepoke真的有挂!菠萝德州... wepoke真的有挂!菠萝德州有挂吗,(德州俱乐部)先前有挂(详细透视分享教程);wpk透视辅助官方...
wepokeai代打!微扑克辅... wepokeai代打!微扑克辅助机器人,(wEpOke)本然是有挂(详细透视攻略方法) 科技详细教程...
wpk有透视辅助!wpk辅助哪... wpk有透视辅助!wpk辅助哪里买,(aa扑克)原来是真的有挂(详细辅助2025新版总结)关于wpk...
aapoker透明挂!德州po... aapoker透明挂!德州poker有外挂吗,(德州wepower)起初有挂(详细透视揭秘攻略);值...
德扑之星猫腻!wpk统计插件,... 德扑之星猫腻!wpk统计插件,(WEPOke)竟然真的有挂(详细透视揭秘教程);一、德扑之星猫腻有挂...
wepoke辅助德之星!wpk... wepoke辅助德之星!wpk外挂被实锤,(AApOKER)固有有挂(详细透视详细教程)关于wepo...
wepoke黑科技!德州扑克后... wepoke黑科技!德州扑克后台监控,(wepokE)固有是有挂(详细透视揭秘教程);是一款可以让一...
wepoke真的有挂!微扑克a... wepoke真的有挂!微扑克ai辅助工具,(aapoker)都是有挂(详细透视高科技教程)关于微扑克...
wepoke辅助机器人!德州a... wepoke辅助机器人!德州aapoker俱乐部外挂,(AAPOKEr)果然真的有挂(详细透视透视教...
德扑之星猫腻!微扑克辅助是什么... 这是一款非常优秀的德扑之星猫腻 ia辅助检测软件,能够让你了解到德扑之星猫腻中牌率当中全部隐藏参数,...