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控制器和视图。

相关内容

热门资讯

事发当天!上饶辅助设备出租,真... 事发当天!上饶辅助设备出租,真是是真的辅助工具(有挂头条)-哔哩哔哩上饶辅助设备出租脚本下载中分为三...
随着!菠萝神辅助器app,一直... 随着!菠萝神辅助器app,一直是有辅助平台(有挂教程)-哔哩哔哩1、很好的工具软件,可以解锁游戏的菠...
推出新举措!兴动海满辅助,一贯... 您好,兴动海满辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在...
做出回应!微乐贵阳捉鸡麻将挂软... 做出回应!微乐贵阳捉鸡麻将挂软件,果然真的是有辅助神器(有挂秘诀)-哔哩哔哩1、打开软件启动之后找到...
反观!九游破解辅助插件,都是真... 反观!九游破解辅助插件,都是真的有辅助神器(有挂秘籍)-哔哩哔哩九游破解辅助插件能透视中分为三种模型...
据文件显示!科米台州麻将辅助,... 据文件显示!科米台州麻将辅助,真是有挂辅助app(竟然有挂)-哔哩哔哩1、每一步都需要思考,不同水平...
今天上午!赣湘互娱挂,总是是有... 今天上午!赣湘互娱挂,总是是有辅助修改器(有挂详情)-哔哩哔哩1、在赣湘互娱挂插件功能辅助器技巧中,...
来临!广西友乐解码器辅助器,原... 来临!广西友乐解码器辅助器,原来真的是有辅助脚本(真的有挂)-哔哩哔哩1.广西友乐解码器辅助器 选牌...
来临!新天道辅助脚本,确实有挂... 来临!新天道辅助脚本,确实有挂辅助下载(有挂秘诀)-哔哩哔哩1、金币登录送、破产送、升级送、活动送。...
近期!青橙竞技卡五星辅助,好像... 近期!青橙竞技卡五星辅助,好像真的是有辅助脚本(竟然有挂)-哔哩哔哩1、用户打开应用后不用登录就可以...