1010cc时时彩标准版 > 三分时时彩1010CC > 1010cc时时彩标准版:多租户的设计与实现,Iden

原标题:1010cc时时彩标准版:多租户的设计与实现,Iden

浏览次数:157 时间:2019-09-23

# identity数据库## 创设空数据库 交给ef管理### 添加asp.net identity包```Install-Package Microsoft.AspNet.Identity.EntityFrameworkInstall-Package Microsoft.AspNet.Identity.OWINInstall-Package Microsoft.Owin.Host.SystemWeb```## 创建owin startup类在app_start目录成立新类 IdentityConfig## 在web.config中增加配置新闻数据库连接字符串。。。。---------------------------## owin配置新闻``````## 创设User类 => 代表客商继承自`Microsoft.AspNet.Identity.EntityFramework`取名空间下的`IdentityUser``IdentityUser`提供了骨干的客商音信,如`email passwordhash Username phonenumber roles`等大家也能够在其派生类中增多额外的音讯```class AppUser:IdentitUser```## 创设Database Context类 =>使用ef code first开管理数据库ASP.Net Identity将使用Code First来创立和管理数据库架构Database Context必需继续自IdentityDbContext,并且T为User类(或然其派生来 这里为AppUser)```class AppIdentityDbContext:IdentityDbContext{ public AppIdentityDbContext() :base("连接字符串key") { } //静态函数只实行贰回,这里运用上面创建的初叶化类来开端化数据库 static AppIdentityDbContext() { Database.SetInitializer(new IdentityDbInit: } //创造Database Context实例 每一趟调用重回三个实例 public static AppIdentityDbContext Create() { return new AppOdentityDbContext(); }}```### 同期能够成立数据库早先化类 这里命名字为IdentityDbInit (承袭自DropCreateDatabaseIfModelChanges这里的泛型T指的是亟需开端化的DbContext)```class IdentityDbInit:DropCreateDatabaseIfModelChanges{ protected override void Seed(AppIdentityDbContext context) { PerformInitialSetup base.Seed } private void PerformInitialSetup(AppIdentityDbContext context) { //实行伊始化操作 }}```## 创制User Manger类User Manager类作为ASP.NET Identity中非常主要的类之一,用来管理User,它提供了成立和操功客商的一对中坚措施,並且周详帮忙C#异步编制程序,值得注意的是,它并不经过Entity Framework来一贯操效能户,而是直接的调用UserStore来达成。UserStore 是Entity Framework类完成了IUserStore接口何况完结了概念在UserManger中操效率户的法子

注:本文是【ASP.NET Identity体系教程】的首先篇。本体系教程详细、完整、深切地介绍了微软的ASP.NET Identity才干,描述了什么样运用ASP.NET Identity完成应用程序的客户管理,以及贯彻应用程序的验证与授权等连锁手艺,译者希望本种类教程能形成左右ASP.NET Identity技能的一份完整而有价值的资料。读者假若可以依据小说的叙说,一边读书、一边推行、一边理解,定能有意料之外的赫赫收获!希望本体系博文能够拿走广泛园友的高度推荐;)。

注:本文是【ASP.NET Identity连串教程】的率先篇。本种类教程详细、完整、深刻地介绍了微软的ASP.NET Identity能力,描述了怎么着利用ASP.NET Identity实现应用程序的客商处理,以及落到实处应用程序的辨证与授权等连锁技艺,译者希望本类别教程能形成控制ASP.NET Identity手艺的一份完整而有价值的材质。读者假诺能够依据小说的叙说,一边读书、一边实施、一边领悟,定能有意外的宏大收获!希望本类别博文可以获取广大园友的惊人推荐

Entity Framework 4.1曾经正式揭露了,新扩展了一种叫做Code First的付出情势。意思正是代码优先;

概要

多租户(Multi Tenancy/Tenant)是一种软件框架结构,其定义是:在一台服务器上运维单个应用实例,它为多少个租户提供劳动。

本框架使用的是分享数据库、分享 Schema、分享数据表的数目陈设框架结构。

隐形斯洛伐克(Slovak)语

掩饰英文

简轻便单的印证:

操作表达

进去系统管理员分界面,展开租户管理界面,如下图所示:

1010cc时时彩标准版 1

下边是租户管理分界面:

1010cc时时彩标准版 2

这里能够管理租户成员,也得以让管理员绑定微信。

下边是大伙儿号配置分界面:

1010cc时时彩标准版 3

那边能够布署大伙儿号的新闻。

系统一管理理员不仅可以够管理自身的租户,还足以管理别的租户内容——大伙儿号处理。

上面是民众号处理分界面:

1010cc时时彩标准版 4

中国和越南社会主义共和国南语对照

中希伯来语对照

预备:您须求已经安装VS二零零六以及Entity Framework 4.1

架构完毕

如上边所述,本框架使用的是分享数据库、共享Schema、分享数据表的数据安插架构。那么,本框架是怎么样贯彻的吧?

主倘使分为以下三步:

  1. 建立TenantId

  2. 扩充ASP.NET Indentity以支撑多租户

  3. 登记租户筛选器

那么首先,这里须要介绍的是TenantId。

13 Getting Started with Identity 13 Identity入门

Identity is a new API from Microsoft to manage users in ASP.NET applications. The mainstay for user management in recent years has been ASP.NET Membership, which has suffered from design choices that were reasonable when it was introduced in 2005 but that have aged badly. The biggest limitation is that the schema used to store the data worked only with SQL Server and was difficult to extend without re-implementing a lot of provider classes. The schema itself was overly complex, which made it harder to implement changes than it should have been.

Identity是微软在ASP.NET应用程序中管理客户的叁个新的API。近年来客商管理的水源一向是ASP.NET的Membership。Membership在二零零七年推出时还算是多个合理的挑选,但近年来线总指挥部的来讲已经严重过时了。它最大的限量是用以存款和储蓄数据的架构(Database Schema)只好选取SQL Server,並且难以扩展,除非重新完结大气的提供器类。其数额架构本人也过于复杂,使之比理论上还要难以达成修改。

Microsoft made a couple of attempts to improve Membership prior to releasing Identity. The first was known as simple membership, which reduced the complexity of the schema and made it easier to customize user data but still needed a relational storage model. The second attempt was the ASP.NET universal providers, which I used in Chapter 10 when I set up SQL Server storage for session data. The advantage of the universal providers is that they use the Entity Framework Code First feature to automatically create the database schema, which made it possible to create databases where access to schema management tools wasn't possible, such as the Azure cloud service. But even with the improvements, the fundamental issues of depending on relational data and difficult customizations remained.

在公布Identity在此以前,微软曾做过三次改革Membership的尝尝。第三个尝试称为Simple Membership(简单成员),它收缩了数据库框架结构的复杂性,并使之轻便定制客商数量,但照旧须要关系型存款和储蓄模型。第贰个尝试是ASP.NET的Universal Providers(通用提供器),第10章在为会话数据建设构造SQL Server存款和储蓄库时曾用过它。Universal Providers的裨益是,那一个提供器使用了Entity Framework的Code First脾性,能够活动地创设数据库架构,使之力所能致在架设管理工科具不恐怕访谈的状态下,比如Azure云服务,也能够创设数据库。但尽管有了改革,其借助于关系型数据以及难以定制等平昔难点如故存在。

To address both problems and to provide a more modern user management platform, Microsoft has replaced Membership with Identity. As you'll learn in this chapter and Chapters 14 and 15, ASP.NET Identity is flexible and extensible, but it is immature, and features that you might take for granted in a more mature system can require a surprising amount of work.

为了解决那多个难点并提供叁个更当代的客商管理平台,微软用Identity替代了Membership。正如就要本章以及第14、15章所通晓到的,ASP.NET Identity灵活且可扩张,但它仍不成熟,你在有个别更成熟的系列中可见拿走的性状,只怕要求超越的专门的职业量。

Microsoft has over-compensated for the inflexibility of Membership and made Identity so open and so adaptable that it can be used in just about any way—just as long as you have the time and energy to implement what you require.

微软现已完全弥补了Membership的不灵活性,使Identity十一分开花和广阔适应,大概能力所能达到以任何方法举行应用——只要你有的时候间有手艺做出你所急需的兑现就可以。

In this chapter, I demonstrate the process of setting up ASP.NET Identity and creating a simple user administration tool that manages individual user accounts that are stored in a database.
在本章中,作者会演示创立ASP.NET Identity的历程,并创办二个简练的客户管理工科具,用以处理存款和储蓄在数据库中的个别顾客账号。

ASP.NET Identity supports other kinds of user accounts, such as those stored using Active Directory, but I don't describe them since they are not used that often outside corporations (where Active Directive implementations tend to be so convoluted that it would be difficult for me to provide useful general examples).
ASP.NET Identity还支持任何品种的顾客账号,举个例子用Active Directory(活动目录)存款和储蓄的账号,但本人不会对其张开描述,因为这种账号日常不会用来集团的外界(这种场地的Active Directory达成多次很复杂,笔者不便提供可行的通用示例)。

In Chapter 14, I show you how to perform authentication and authorization using those user accounts, and in Chapter 15, I show you how to move beyond the basics and apply some advanced techniques. Table 13-1 summarizes this chapter.

在第14章中,小编将演示怎么样用那么些客户账号进行认证与授权,第15章将演示怎么样步向高等论题,运用一些高等才干。表13-1是本章概要。

Table 13-1. Chapter Summary
表13-1. 本章概要
Problem
问题
Solution
解决方案
Listing
清单号
Install ASP.NET Identity.
安装ASP.NET Identity
Add the NuGet packages and define a connection string and an OWIN start class in the Web.config file.
添加NuGet包,并在Web.config文件中定义一个链接字符串和一个OWIN启动类
1–4
Prepare to use ASP.NET Identity.
使用ASP.NET Identity的准备
Create classes that represent the user, the user manager, the database context, and the OWIN start class.
创建表示用户、用户管理器、数据库上下文的类,以及OWIN类
5–8
Enumerate user accounts.
枚举用户账号
Use the Users property defined by the user manager class.
使用由用户管理器类定义的Users属性
9, 10
Create user accounts.
创建用户账号
Use the CreateAsync method defined by the user manager class.
使用由用户管理器类定义的CreateAsync方法
11–13
Enforce a password policy.
强制口令策略
Set the PasswordValidator property defined by the user manager class, either using the built-in PasswordValidator class or using a custom derivation.
设置由用户管理器类定义的PasswordValidator属性,既可以使用内建的PasswordValidator类,也可以使用自定义的派生类。
14–16
Validate new user accounts.
验证新的用户账号
Set the UserValidator property defined by the user manager class, either using the built-in UserValidator class or using a custom derivation.
设置由用户管理器类定义的UserValidator属性,既可以使用内建的UserValidator类,也可以使用自定义的派生类。
17–19
Delete user accounts.
删除用户账号
Use the DeleteAsync method defined by the user manager class.
使用由用户管理器定义的DeleteAsync方法
20–22
Modify user accounts.
修改用户账号
Use the UpdateAsync method defined by the user manager class.
使用由用户管理器类定义的UpdateAsync方法
23–24

13 Getting Started with Identity 13 Identity入门

Identity is a new API from Microsoft to manage users in ASP.NET applications. The mainstay for user management in recent years has been ASP.NET Membership, which has suffered from design choices that were reasonable when it was introduced in 2005 but that have aged badly. The biggest limitation is that the schema used to store the data worked only with SQL Server and was difficult to extend without re-implementing a lot of provider classes. The schema itself was overly complex, which made it harder to implement changes than it should have been.
Identity是微软在ASP.NET应用程序中管理客户的贰个新的API。近期客商管理的木本一直是ASP.NET的Membership。Membership在2006年推出时还算是贰个客观的挑三拣四,但当下看来已经严重过时了。它最大的范围是用以存款和储蓄数据的架构(Database Schema)只好接纳SQL Server,而且难以扩充,除非重新完毕大气的提供器类。其数额架构自个儿也过于复杂,使之比理论上还要难以实现修改。

Microsoft made a couple of attempts to improve Membership prior to releasing Identity. The first was known as simple membership, which reduced the complexity of the schema and made it easier to customize user data but still needed a relational storage model. The second attempt was the ASP.NET universal providers, which I used in Chapter 10 when I set up SQL Server storage for session data. The advantage of the universal providers is that they use the Entity Framework Code First feature to automatically create the database schema, which made it possible to create databases where access to schema management tools wasn't possible, such as the Azure cloud service. But even with the improvements, the fundamental issues of depending on relational data and difficult customizations remained.
在颁发Identity从前,微软曾做过一回改革Membership的尝试。第叁个尝试称为Simple Membership,它减弱了数据库架构的繁杂,并使之轻便定制客户数据,但依然必要关系型存款和储蓄模型。第一个尝试是ASP.NET的Universal Providers,第10章在为会话数据组建SQL Server存储库时曾用过它。Universal Providers的裨益是,这个提供器使用了Entity Framework的Code First个性,能够活动地创设数据库架构,使之能力所能达到在架设管理工科具相当小概访谈的状态下,比方Azure云服务,也能够创设数据库。但就是有了改革,其借助于关系型数据以及难以定制等平昔难题依旧存在。

To address both problems and to provide a more modern user management platform, Microsoft has replaced Membership with Identity. As you'll learn in this chapter and Chapters 14 and 15, ASP.NET Identity is flexible and extensible, but it is immature, and features that you might take for granted in a more mature system can require a surprising amount of work.
为了消除那多个难点并提供二个更现代的顾客管理平台,微软用Identity替代了Membership。正如就要本章以及第14、15章所精通到的,ASP.NET Identity灵活且可扩充,但它仍不成熟,你在有个别更成熟的系统中能够拿走的性状,大概要求赶过的工作量。

Microsoft has over-compensated for the inflexibility of Membership and made Identity so open and so adaptable that it can be used in just about any way—just as long as you have the time and energy to implement what you require.
微软现已完全弥补了Membership的不灵活性,使Identity十三分开花和周边适应,大致能力所能达到以其余措施开展应用——只要你有的时候光有力量做出你所急需的兑现就可以。

In this chapter, I demonstrate the process of setting up ASP.NET Identity and creating a simple user administration tool that manages individual user accounts that are stored in a database.
在本章中,作者会演示创设ASP.NET Identity的经过,并创办多少个简易的顾客管理工具,用以管理存款和储蓄在数据库中的个别客商账号。

ASP.NET Identity supports other kinds of user accounts, such as those stored using Active Directory, but I don't describe them since they are not used that often outside corporations (where Active Directive implementations tend to be so convoluted that it would be difficult for me to provide useful general examples).
ASP.NET Identity还援助任何品种的客商账号,举个例子用Active Directory存款和储蓄的账号,但自个儿不会对其开展描述,因为这种账号平常不会用来公司的外界(这种场馆的Active Directory完毕数十三遍很复杂,小编不便提供立见成效的通用示例)。

In Chapter 14, I show you how to perform authentication and authorization using those user accounts, and in Chapter 15, I show you how to move beyond the basics and apply some advanced techniques. Table 13-1 summarizes this chapter.
在第14章中,笔者将演示怎样用这么些顾客账号进行认证与授权,第15章将演示怎么着步入高端论题,运用一些高档技术。表13-1是本章概要。

Table 13-1. Chapter Summary
表13-1. 本章概要
Problem
问题
Solution
解决方案
Listing
清单号
Install ASP.NET Identity.
安装ASP.NET Identity
Add the NuGet packages and define a connection string and an OWIN start class in the Web.config file.
添加NuGet包,并在Web.config文件中定义一个链接字符串和一个OWIN启动类
1–4
Prepare to use ASP.NET Identity.
使用ASP.NET Identity的准备
Create classes that represent the user, the user manager, the database context, and the OWIN start class.
创建表示用户、用户管理器、数据库上下文的类,以及OWIN类
5–8
Enumerate user accounts.
枚举用户账号
Use the Users property defined by the user manager class.
使用由用户管理器类定义的Users属性
9, 10
Create user accounts.
创建用户账号
Use the CreateAsync method defined by the user manager class.
使用由用户管理器类定义的CreateAsync方法
11–13
Enforce a password policy.
强制口令策略
Set the PasswordValidator property defined by the user manager class, either using the built-in PasswordValidator class or using a custom derivation.
设置由用户管理器类定义的PasswordValidator属性,既可以使用内建的PasswordValidator类,也可以使用自定义的派生类。
14–16
Validate new user accounts.
验证新的用户账号
Set the UserValidator property defined by the user manager class, either using the built-in UserValidator class or using a custom derivation.
设置由用户管理器类定义的UserValidator属性,既可以使用内建的UserValidator类,也可以使用自定义的派生类。
17–19
Delete user accounts.
删除用户账号
Use the DeleteAsync method defined by the user manager class.
使用由用户管理器定义的DeleteAsync方法
20–22
Modify user accounts.
修改用户账号
Use the UpdateAsync method defined by the user manager class.
使用由用户管理器类定义的UpdateAsync方法
23–24

1、新建项目或网址

树立租户Id(TenantId)

我们先来寻访租户表:

/// <summary>
    /// 租户信息
    /// </summary>
    public class Account_Tenant
    {
        /// <summary>
        /// 多租户Id
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 是否为系统租户(仅支持一个)
        /// </summary>
        public bool IsSystemTenant { get; set; }
        /// <summary>
        /// 租户名称
        /// </summary>
        [Display(Name = "名称")]
        [Required]
        [MaxLength(20)]
        public string Name { get; set; }
        [Display(Name = "备注")]
        [DataType(DataType.MultilineText)]
        [MaxLength(500)]
        public string Remark { get; set; }
}

如上所示,Id为主键,标记列,由数据库自动生成(EF Code First方式下,暗中认可Id为主键,int类型主键自动安装为标记列)。

那么,租户Id产生了后头,全部租户分享数据表贮存数据,分化租户的数据须求通过 TenantId 字段来区别。

大家来看二个基类的统一准备:

public abstract class WeiChat_TenantBase<TKey> : ITenantId, IAdminCreate<string>, IAdminUpdate<string>
    {
        [Key]
        public virtual TKey Id { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [Display(Name = "创建时间")]
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 更新时间
        /// </summary>
        [Display(Name = "更新时间")]
        public DateTime? UpdateTime { get; set; }
        /// <summary>
        /// 创建者
        /// </summary>
        [MaxLength(128)]
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建者
        /// </summary>
        [Display(Name = "创建者")]
        //[NotMapped]
        [ForeignKey("CreateBy")]
        public AppUser CreateUser { get; set; }

        /// <summary>
        /// 更新者
        /// </summary>
        [MaxLength(128)]
        public string UpdateBy { get; set; }
        /// <summary>
        /// 编辑者
        /// </summary>
        [MaxLength(256)]
        [Display(Name = "最后编辑")]
        //[NotMapped]
        public AppUser UpdateUser { get; set; }

        public int TenantId { get; set; }
}

如上所示,TenantId正是数据的山岭,分裂数量的筛选须要依靠其来筛选。

如下图的规划:

1010cc时时彩标准版 5

从上海体育场面能够看来,那块千头万绪的类都缺不了TenantId,恐怕看类依旧不太精晓,我们来看表结构吧,比方说:

1010cc时时彩标准版 61010cc时时彩标准版 71010cc时时彩标准版 81010cc时时彩标准版 9

等等。如上边表结构所示,TenantId为个表间必备字段。

而在Code First形式下,使用持续可以很有利的将持有的模子类增加相关字段。

猛烈,本框架使用了ASP.NET Indentity,那么什么样对ASP.NET Indentity达成多租户的恢弘呢?

13.1 Preparing the Example Project 13.1 筹划示例项目

I created a project called Users for this chapter, following the same steps I have used throughout this book. I selected the Empty template and checked the option to add the folders and references required for an MVC application. I will be using Bootstrap to style the views in this chapter, so enter the following command into the Visual Studio Package Manager Console and press Enter to download and install the NuGet package:
本章依据本书一贯采用的平等步骤创设了贰个称呼为Users的品种。在创设进度中选择了“Empty(空)” 模板,并在“Add the folders and references(添Gavin书夹和援引)”中选中了“MVC”复选框。本章将选拔Bootstrap来安装视图的样式,由此在Visual Studio的“Package Manager Console(包处理器调节台)”中输入以下命令,并按回车,下载并设置那一个NuGet包。

Install-Package -version 3.0.3 bootstrap

I created a Home controller to act as the focal point for the examples in this chapter. The definition of the controller is shown in Listing 13-1. I'll be using this controller to describe details of user accounts and data, and the Index action method passes a dictionary of values to the default view via the View method.
笔者创设了Home调节器,以作为本章示例的要害。该调整器的定义如清单13-1所示。此调控器将用来描述顾客账号的内幕和数码,Index动作方法通过View方法给默许视图传递了七个字典值。

Listing 13-1. The Contents of the HomeController.cs File
清单13-1. HomeController.cs文件的剧情

using System.Web.Mvc;
using System.Collections.Generic;

namespace Users.Controllers {

    public class HomeController : Controller {

        public ActionResult Index() {
            Dictionary<string, object> data
                = new Dictionary<string, object>();
            data.Add("Placeholder", "Placeholder");
            return View(data);
        }
    }
}

I created a view by right-clicking the Index action method and selecting Add View from the pop-up menu. I set View Name to Index and set Template to Empty (without model). Unlike the examples in previous chapters, I want to use a common layout for this chapter, so I checked the Use a Layout Page option. When I clicked the Add button, Visual Studio created the Views/Shared/_Layout.cshtml and Views/Home/Index.cshtml files. Listing 13-2 shows the contents of the _Layout.cshtml file.
通过右击Index动作方法,并从弹出美食指南选用“Add View(加多视图)”,笔者创制了贰个视图。将“View Name(视图名称)”设置为“Index”,并将“Template(模板)”设置为“空(无模型)”。与近年来几章的亲自去做分化,本章希望利用三个通用的布局,于是选中了“Use a Layout Page(使用布局页面)”复选框。点击“Add(增多)”开关后,Visual Studio创立了Views/Shared/_Layout.cshtml和Views/Home/Index.cshtml文件。清单13-2显示了_Layout.cshtml文件的内容。

Listing 13-2. The Contents of the _Layout.cshtml File
清单13-2. _Layout.cshtml文件的从头到尾的经过

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    <link href="~/Content/bootstrap-theme.min.css" rel="stylesheet" />
    <style>
        .container { padding-top: 10px; }
        .validation-summary-errors { color: #f00; }
    </style>
</head>
<body class="container">
    <div class="container">
        @RenderBody()
    </div>
</body>
</html>

Listing 13-3 shows the contents of the Index.cshtml file.
清单13-3呈现了Index.cshtml文件的开始和结果。

Listing 13-3. The Contents of the Index.cshtml File
清单13-3. Index.cshtml文件的剧情

@{
    ViewBag.Title = "Index";
}

<div class="panel panel-primary">
    <div class="panel-heading">User Details</div>
    <table class="table table-striped">
        @foreach (string key in Model.Keys) {
            <tr>
                <th>@key</th>
                <td>@Model[key]</td>
            </tr>
        }
    </table>
</div>

To test that the example application is working, select Start Debugging from the Visual Studio Debug menu and navigate to the /Home/Index URL. You should see the result illustrated by Figure 13-1.
为了测量试验该应用程序示例能够专门的工作,从Visual Studio的“Debug(调试)”菜单中选取“Start Debugging(运行调节和测量试验)”,并导航到/Home/Index网站,便得以观望如图13-1所示的结果。

1010cc时时彩标准版 10

Figure 13-1. Testing the example application
图13-1. 测验示例应用程序

13.1 Preparing the Example Project 13.1 希图示例项目

I created a project called Users for this chapter, following the same steps I have used throughout this book. I selected the Empty template and checked the option to add the folders and references required for an MVC application. I will be using Bootstrap to style the views in this chapter, so enter the following command into the Visual Studio Package Manager Console and press Enter to download and install the NuGet package:
本章依据本书平素采纳的一模一样步骤创立了一个称呼为Users的门类。在开立进程中选拔了“Empty” 模板,并在“Add the folders and references”中选中了“MVC”复选框。本章将使用Bootstrap来安装视图的体裁,因而在Visual Studio的“Package Manager Console”中输入以下命令,并按回车,下载并安装那一个NuGet包。

Install-Package -version 3.0.3 bootstrap

I created a Home controller to act as the focal point for the examples in this chapter. The definition of the controller is shown in Listing 13-1. I'll be using this controller to describe details of user accounts and data, and the Index action method passes a dictionary of values to the default view via the View method.
自家创设了Home调整器,以作为本章示例的刀口。该调节器的概念如清单13-1所示。此调控器将用来说述客户账号的内部意况和数码,Index动作方法通过View方法给私下认可视图传递了叁个字典值。

Listing 13-1. The Contents of the HomeController.cs File
清单13-1. HomeController.cs文件的内容

using System.Web.Mvc;using System.Collections.Generic;
namespace Users.Controllers {
    public class HomeController : Controller {
        public ActionResult Index() {            Dictionary<string, object> data                = new Dictionary<string, object>();            data.Add("Placeholder", "Placeholder");            return View;        }    }}

I created a view by right-clicking the Index action method and selecting Add View from the pop-up menu. I set View Name to Index and set Template to Empty (without model). Unlike the examples in previous chapters, I want to use a common layout for this chapter, so I checked the Use a Layout Page option. When I clicked the Add button, Visual Studio created the Views/Shared/_Layout.cshtml and Views/Home/Index.cshtml files. Listing 13-2 shows the contents of the _Layout.cshtml file.
通过右击Index动作方法,并从弹出美食指南接纳“Add View”,小编创设了八个视图。将“View Name”设置为“Index”,并将“Template”设置为“空”。与日前几章的演示分化,本章希望利用三个通用的布局,于是选中了“Use a Layout Page”复选框。点击“Add”开关后,Visual Studio创立了Views/Shared/_Layout.cshtml和Views/Home/Index.cshtml文件。清单13-2显示了_Layout.cshtml文件的原委。

Listing 13-2. The Contents of the _Layout.cshtml File
清单13-2. _Layout.cshtml文件的剧情

<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>@ViewBag.Title</title>    <link href="~/Content/bootstrap.min.css" rel="stylesheet" />    <link href="~/Content/bootstrap-theme.min.css" rel="stylesheet" />    <style>        .container { padding-top: 10px; }        .validation-summary-errors { color: #f00; }    </style></head><body >    <div >        @RenderBody()    </div></body></html>

Listing 13-3 shows the contents of the Index.cshtml file.
清单13-3出示了Index.cshtml文件的剧情。

Listing 13-3. The Contents of the Index.cshtml File
清单13-3. Index.cshtml文件的内容

@{    ViewBag.Title = "Index";}
<div >    <div >User Details</div>    <table >        @foreach (string key in Model.Keys) {            <tr>                <th>@key</th>                <td>@Model[key]</td>            </tr>        }    </table></div>

To test that the example application is working, select Start Debugging from the Visual Studio Debug menu and navigate to the /Home/Index URL. You should see the result illustrated by Figure 13-1.
为了测量试验该应用程序示例能够职业,从Visual Studio的“Debug”菜单中选择“Start Debugging”,并导航到/Home/Index网站,便足以见见如图13-1所示的结果。

1010cc时时彩标准版 11

Figure 13-1. Testing the example application
图13-1. 测量试验示例应用程序

不论是网站,照旧项目都足以选取Code First的开销格局。

增加ASP.NET Indentity以援救多租户

在本框架中,编写了库Magicodes.WeiChat.Data.Multitenant,用于扩大ASP.NET Indentity以帮衬多租户。

动用过ASP.NET Indentity的朋友应该都精晓Microsoft.AspNet.Identity.EntityFramework——ASP.NET Indentity使用EF作为其数据存款和储蓄的完成库。通过对象浏览器查看,轻便看出,其珍视定义了以下指标:

1010cc时时彩标准版 12

中间,IdentityDbContext 承接自System.Data.Entity.DbContext,具体定义如下所示:

public class IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> : System.Data.Entity.DbContext

where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>

where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityRole<TKey, TUserRole>

where TUserLogin : Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>

where TUserRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>

where TUserClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>

Microsoft.AspNet.Identity.EntityFramework 的成员

那么,大家未来的基本点对象正是解决他们了。

1010cc时时彩标准版 13

梯次对应提到如下所示:

1010cc时时彩标准版 14

如上所示,通过扩充ASP.NET Identity的IUser、IdentityUser、IdentityDbContext、IdentityUserLogin、UserStore来成功了对多租户的支撑,相同的时间须要细心的是,还要重写方法FindByNameAsync、AddLoginAsync、FindAsync、FindByEmailAsync、CreateAsync,以支撑多租户。

达成了对ASP.NET Identity的多租户的支撑,大家还索要对数据实行筛选,不过富有地方都助长筛选代码是一件很困苦的事务,何况在编辑逻辑的时候还很轻便骨痿,那么有怎么样好的措施呢?是时候祭出大家的神器了——EntityFramework.DynamicFilters。

13.2 Setting Up ASP.NET Identity 13.2 建立ASP.NET Identity

For most ASP.NET developers, Identity will be the first exposure to the Open Web Interface for .NET (OWIN). OWIN is an abstraction layer that isolates a web application from the environment that hosts it. The idea is that the abstraction will allow for greater innovation in the ASP.NET technology stack, more flexibility in the environments that can host ASP.NET applications, and a lighter-weight server infrastructure.
对于好些个ASP.NET开垦者来讲,Identity将是率先个暴光给OWIN(Open Web Interface for .NET——.NET开放Web接口)的组件。OWIN是多个将Web应用程序从托管它的条件中独立出来的抽象层。其构思是其一独自出来的抽象层能够使ASP.NET技术饭店有越来越大的翻新,使托管ASP.NET应用程序的条件有越多的狡猾,并能够是轻量级的服务器架设。

OWIN is an open standard (which you can read at ). Microsoft has created Project Katana, its implementation of the OWIN standard and a set of components that provide the functionality that web applications require. The attraction to Microsoft is that OWIN/Katana isolates the ASP.NET technology stack from the rest of the .NET Framework, which allows a greater rate of change.

OWIN是叁个吐放标准(参阅

OWIN developers select the services that they require for their application, rather than consuming an entire platform as happens now with ASP.NET. Individual services—known as middleware in the OWIN terminology—can be developed at different rates, and developers will be able to choose between providers for different services, rather than being tied to a Microsoft implementation.

OWIN开辟人士为他们的应用程序选取所需的劳动,并不是像明日那般只是地运用ASP.NET平台。个别服务——在OWIN术语中称之为“Middleware(中间件)”——可能会有不一样的开拓速率,而开采人士将能够在分裂的服务提供商之间开展抉择,并非被绑定在微软落到实处上。

There is a lot to like about the direction that OWIN and Katana are heading in, but it is in the early days, and it will be some time before it becomes a complete platform for ASP.NET applications. As I write this, it is possible to build Web API and SignalR applications without needing the System.Web namespace or IIS to process requests, but that's about all. The MVC framework requires the standard ASP.NET platform and will continue to do so for some time to come.

OWIN和Katana有许多喜爱发展的矛头,但它仍处在中期时代,还要求一段时间才能成为ASP.NET应用程序的完全平台。在自家编写本书的时候,它已能够在不必要System.Web命名空间照旧IIS管理央求的情事下,创立Web API和Signal本田UR-V应用程序了。MVC框架要求标准的ASP.NET平台,因而在一段时间内仍将沿用今后的做法。

The ASP.NET platform and IIS are not going away. Microsoft has been clear that it sees one of the most attractive aspects of OWIN as allowing developers more flexibility in which middleware components are hosted by IIS, and Project Katana already has support for the System.Web namespaces. OWIN and Katana are not the end of ASP.NET—rather, they represent an evolution where Microsoft allows developers more flexibility in how ASP.NET applications are assembled and executed.

ASP.NET平台以及IIS不会收敛。微软已经知道地来看,OWIN最有吸重力的三个地方是开辟者具有了更加大的狡猾,中间组件可以由IIS来托管,而Katana项目早就实现了对System.Web命名空间的支撑。OWIN和Katana不是ASP.NET的了断——而是预示着一种变革,微软让开垦人士能够在ASP.NET应用程序的编写翻译和施行方面更灵敏。

Tip Identity is the first major ASP.NET component to be delivered as OWIN middleware, but it won't be the last. Microsoft has made sure that the latest versions of Web API and SignalR don't depend on the System.Web namespaces, and that means that any component intended for use across the ASP.NET family of technologies has to be delivered via OWIN. I get into more detail about OWIN in my Expert ASP.NET Web API 2 for MVC Developers book, which will be published by Apress in 2014.

提示:Identity是用作OWIN中间件而付出的第四个基本点的ASP.NET组件,但这不是最终二个。微软已经保证,Web API和Signal奥迪Q5的新式版本不会凭仗于System.Web命名空间,那表示,筹算交叉使用ASP.NET家族手艺的别的组件,都无法不经过OWIN进行交付。关于OWIN,在自个儿的Expert ASP.NET Web API 2 for MVC Developers一书中有更详细的阐释,该书已由Apress于二〇一四年问世。

OWIN and Katana won't have a major impact on MVC framework developers for some time, but changes are already taking effect—and one of these is that ASP.NET Identity is implemented as an OWIN middleware component. This isn't ideal because it means that MVC framework applications have to mix OWIN and traditional ASP.NET platform techniques to use Identity, but it isn't too burdensome once you understand the basics and know how to get OWIN set up, which I demonstrate in the sections that follow.

OWIN和Katana在一段时间内还不会对MVC框架开拓职员产生重大冲击,但调换正在发生——个中之一就是ASP.NET Identity要作为OWIN中间件而达成。这种处境不太意得志满,因为那意味着,MVC框架的应用程序为了利用Identity,必需将OWIN和守旧的ASP.NET平台混合着搭配在协同,那太繁琐了,在你知道了基本概念并且知道怎么建构OWIN(以下几小节演示)之后,便会领会。

13.2 Setting Up ASP.NET Identity 13.2 建立ASP.NET Identity

For most ASP.NET developers, Identity will be the first exposure to the Open Web Interface for .NET . OWIN is an abstraction layer that isolates a web application from the environment that hosts it. The idea is that the abstraction will allow for greater innovation in the ASP.NET technology stack, more flexibility in the environments that can host ASP.NET applications, and a lighter-weight server infrastructure.
对此大许多ASP.NET开拓者而言,Identity将是首先个暴光给OWIN(Open Web Interface for .NET——.NET开放Web接口)的零部件。OWIN是一个将Web应用程序从托管它的蒙受中独立出来的抽象层。其观念是这么些独自出来的抽象层能够使ASP.NET本领仓库有越来越大的换代,使托管ASP.NET应用程序的条件有越来越多的油滑,并能够是轻量级的服务器架设。

OWIN is an open standard (which you can read at ). Microsoft has created Project Katana, its implementation of the OWIN standard and a set of components that provide the functionality that web applications require. The attraction to Microsoft is that OWIN/Katana isolates the ASP.NET technology stack from the rest of the .NET Framework, which allows a greater rate of change.
OWIN是三个盛开标准(参阅

OWIN developers select the services that they require for their application, rather than consuming an entire platform as happens now with ASP.NET. Individual services—known as middleware in the OWIN terminology—can be developed at different rates, and developers will be able to choose between providers for different services, rather than being tied to a Microsoft implementation.
OWIN开垦职员为她们的应用程序选拔所需的服务,实际不是像现在如此只是地选择ASP.NET平台。个别服务——在OWIN术语中称之为“Middleware”——恐怕会有两样的开销速率,而开采人士将能够在差异的服务提供商之间张开选拔,实际不是被绑定在微软完结上。

There is a lot to like about the direction that OWIN and Katana are heading in, but it is in the early days, and it will be some time before it becomes a complete platform for ASP.NET applications. As I write this, it is possible to build Web API and SignalR applications without needing the System.Web namespace or IIS to process requests, but that's about all. The MVC framework requires the standard ASP.NET platform and will continue to do so for some time to come.
OWIN和Katana有许多欢乐发展的可行性,但它仍居于刚同志开始阶段时代,还索要一段时间技巧成为ASP.NET应用程序的完全平台。在自己编写本书的时候,它已足以在无需System.Web命名空间依旧IIS管理供给的动静下,创建Web API和Signal奇骏应用程序了。MVC框架要求正统的ASP.NET平台,因而在一段时间内仍将沿用现在的做法。

The ASP.NET platform and IIS are not going away. Microsoft has been clear that it sees one of the most attractive aspects of OWIN as allowing developers more flexibility in which middleware components are hosted by IIS, and Project Katana already has support for the System.Web namespaces. OWIN and Katana are not the end of ASP.NET—rather, they represent an evolution where Microsoft allows developers more flexibility in how ASP.NET applications are assembled and executed.
ASP.NET平台以及IIS不会消退。微软早就清楚地看到,OWIN最有吸重力的三个上边是开采者具备了更加大的灵活性,中间组件能够由IIS来托管,而Katana项目早已实现了对System.Web命名空间的支撑。OWIN和Katana不是ASP.NET的终止——而是预示着一种变革,微软让开采职员能够在ASP.NET应用程序的编写翻译和实施方面进一步灵敏。

Tip Identity is the first major ASP.NET component to be delivered as OWIN middleware, but it won't be the last. Microsoft has made sure that the latest versions of Web API and SignalR don't depend on the System.Web namespaces, and that means that any component intended for use across the ASP.NET family of technologies has to be delivered via OWIN. I get into more detail about OWIN in my Expert ASP.NET Web API 2 for MVC Developers book, which will be published by Apress in 2014.
提示:Identity是作为OWIN中间件而付出的第二个首要的ASP.NET组件,但这不是最终一个。微软早就保障,Web API和SignalHaval的摩登版本不会依赖于System.Web命名空间,那意味着,计划交叉使用ASP.NET家族技艺的别样组件,都无法不通过OWIN进行交付。关于OWIN,在本身的Expert ASP.NET Web API 2 for MVC Developers一书中有更详细的阐述,该书已由Apress于二〇一五年出版。

OWIN and Katana won't have a major impact on MVC framework developers for some time, but changes are already taking effect—and one of these is that ASP.NET Identity is implemented as an OWIN middleware component. This isn't ideal because it means that MVC framework applications have to mix OWIN and traditional ASP.NET platform techniques to use Identity, but it isn't too burdensome once you understand the basics and know how to get OWIN set up, which I demonstrate in the sections that follow.
OWIN和Katana在一段时间内还不会对MVC框架开采职员发生根本冲击,但转换正在产生——当中之一就是ASP.NET Identity要作为OWIN中间件而落实。这种场合不太优秀,因为这象征,MVC框架的应用程序为了利用Identity,必须将OWIN和历史观的ASP.NET平台混合着搭配在一齐,那太烦琐了,在你领会了基本概念并且知道什么树立OWIN之后,便会清楚。

2、增添类库援引

登记租户筛选器

筛选器依赖ENTITYFRAMEWOKugaK.DYNAMICFILTE纳瓦拉S,那是五个开源项目,相关介绍能够访问以下链接:

此间,大家定义了之类租户筛选器:

modelBuilder.Filter("TenantEntryFilter", (ITenantId app, int tenantId) => (app.TenantId == tenantId), 0);

下一场我们得以应用以下代码来启用筛选器:

db.EnableFilter(tenantFilterName);

//设置多租户过滤

db.SetFilterScopedParameterValue(tenantFilterName, "tenantId", TenantId);

上述代码大家能够写到通用的地点开展打包,譬如调控器基类的OnActionExecuting方法中。

13.2.1 Creating the ASP.NET Identity Database 13.2.1 创建ASP.NET Identity数据库

ASP.NET Identity isn't tied to a SQL Server schema in the same way that Membership was, but relational storage is still the default—and simplest—option, and it is the one that I will be using in this chapter. Although the NoSQL movement has gained momentum in recent years, relational databases are still the mainstream storage choice and are well-understood in most development teams.
ASP.NET Identity并未有像Membership那样,被绑定到SQL Server架构,但关系型存款和储蓄仍是暗中认可的,并且是最简便易行的选料,那也是本章中将使用的样式。就算最近现身了NoSQL运动方向,但关系型数据库依然是生死攸关的储存选拔,並且大多数付出公司都有卓绝掌握。

ASP.NET Identity uses the Entity Framework Code First feature to automatically create its schema, but I still need to create the database into which that schema—and the user data—will be placed, just as I did in Chapter 10 when I created the database for session state data (the universal provider that I used to manage the database uses the same Code First feature).
ASP.NET Identity使用Entity Framework的Code First个性自动地开创它的数目架构(Schema),但自己如故必要制造一个用来放置此数据架构以及顾客数据的数据库,仿佛第10章所做的那样,当时为会话状态数据创造了数据库(用来管理数据库的通用提供器同样利用了Code First个性)。

Tip You don't need to understand how Entity Framework or the Code First feature works to use ASP.NET Identity.
提示:为了利用ASP.NET Identity,不自然要知道Entity Framework或Code First脾气的做事机制。

As in Chapter 10, I will be using the localdb feature to create my database. As a reminder, localdb is included in Visual Studio and is a cut-down version of SQL Server that allows developers to easily create and work with databases.
正如第10章那样,小编将利用localdb天性来创建数据库。要切记的是,localdb是带有在Visual Studio之中的,而且是三个简化版的SQL Server,能够让开垦者方便地创制和选拔数据库。

Select SQL Server Object Explorer from the Visual Studio View menu and right-click the SQL Server object in the window that appears. Select Add SQL Server from the pop-up menu, as shown in Figure 13-2.
从Visual Studio的“View(视图)”菜单中采取“SQL Server Object Explorer(SQL Server对象能源管理器)”,并在所出现的窗口中右击“SQL Server”对象。从弹出美食做法选用“Add SQL Server(增加SQL Server)”,如图13-2所示。

1010cc时时彩标准版 15

Figure 13-2. Creating a new database connection
图13-2. 成立三个新的数据库连接

Visual Studio will display the Connect to Server dialog. Set the server name to (localdb)v11.0, select the Windows Authentication option, and click the Connect button. A connection to the database will be established and shown in the SQL Server Object Explorer window. Expand the new item, right-click Databases, and select Add New Database from the pop-up window, as shown in Figure 13-3.
Visual Studio将体现“Connect to Server(连接到服务器)”对话框,将服务器名称设置为(localdb)v11.0,选取“Windows Authentication(Windows认证)”选项,点击“Connect(连接)”按键。那将创造二个数据库连接,并出示在“SQL Server对象能源管理器”窗口中。张开那几个新项,右击“Databases(数据库)”,并从弹出美食指南选取“Add New Database(增多新数据库)”,如图13-3所示。

1010cc时时彩标准版 16

Figure 13-3. Adding a new database
图13-3. 增加新数据库

Set the Database Name option to IdentityDb, leave the Database Location value unchanged, and click the OK button to create the database. The new database will be shown in the Databases section of the SQL connection in the SQL Server Object Explorer.
将“Database Name(数据库名称)”选项设置为“IdentityDb”,不用改动“Database Location(数据库地点)”的值,点击OK开关创立此数据库。这一个新数据库将应时而生在“SQL Server对象财富管理器”中“SQL连接”的“数据库”小节中。

13.2.1 Creating the ASP.NET Identity Database 13.2.1 创建ASP.NET Identity数据库

ASP.NET Identity isn't tied to a SQL Server schema in the same way that Membership was, but relational storage is still the default—and simplest—option, and it is the one that I will be using in this chapter. Although the NoSQL movement has gained momentum in recent years, relational databases are still the mainstream storage choice and are well-understood in most development teams.
ASP.NET Identity并未有像Membership那样,被绑定到SQL Server架构,但关系型存款和储蓄仍是暗许的,並且是最轻巧易行的选料,那也是本章旅长使用的款式。纵然近期冒出了NoSQL运动方向,但关系型数据库依旧是主要的囤积选用,何况大繁多付出集团都有漂亮掌握。

ASP.NET Identity uses the Entity Framework Code First feature to automatically create its schema, but I still need to create the database into which that schema—and the user data—will be placed, just as I did in Chapter 10 when I created the database for session state data (the universal provider that I used to manage the database uses the same Code First feature).
ASP.NET Identity使用Entity Framework的Code First天性自动地创制它的多少架构,但自己依旧须求创造四个用来放置此数据架构以及顾客数量的数据库,就疑似第10章所做的那么,当时为会话状态数据成立了数据库(用来治本数据库的通用提供器一样利用了Code First性子)。

Tip You don't need to understand how Entity Framework or the Code First feature works to use ASP.NET Identity.
提示:为了利用ASP.NET Identity,不自然要知道Entity Framework或Code First性子的做事体制。

As in Chapter 10, I will be using the localdb feature to create my database. As a reminder, localdb is included in Visual Studio and is a cut-down version of SQL Server that allows developers to easily create and work with databases.
正如第10章那样,笔者将运用localdb本性来创立数据库。要铭记的是,localdb是带有在Visual Studio之中的,而且是一个简化版的SQL Server,能够让开荒者方便地开创和接纳数据库。

Select SQL Server Object Explorer from the Visual Studio View menu and right-click the SQL Server object in the window that appears. Select Add SQL Server from the pop-up menu, as shown in Figure 13-2.
从Visual Studio的“View”菜单中选取“SQL Server Object Explorer(SQL Server对象财富管理器)”,并在所出现的窗口中右击“SQL Server”对象。从弹出菜谱接纳“Add SQL Server(增多SQL Server)”,如图13-2所示。

1010cc时时彩标准版 17

Figure 13-2. Creating a new database connection
图13-2. 成立三个新的数据库连接

Visual Studio will display the Connect to Server dialog. Set the server name to v11.0, select the Windows Authentication option, and click the Connect button. A connection to the database will be established and shown in the SQL Server Object Explorer window. Expand the new item, right-click Databases, and select Add New Database from the pop-up window, as shown in Figure 13-3.
Visual Studio将显得“Connect to Server”对话框,将服务器名称设置为v11.0,选用“Windows Authentication(Windows认证)”选项,点击“Connect”按键。这将确立一个数据库连接,并出示在“SQL Server对象财富管理器”窗口中。展开那些新项,右击“Databases”,并从弹出菜谱选用“Add New Database”,如图13-3所示。

1010cc时时彩标准版 18

Figure 13-3. Adding a new database
图13-3. 增添新数据库

Set the Database Name option to IdentityDb, leave the Database Location value unchanged, and click the OK button to create the database. The new database will be shown in the Databases section of the SQL connection in the SQL Server Object Explorer.
将“Database Name”选项设置为“IdentityDb”,不用退换“Database Location”的值,点击OK开关创制此数据库。这几个新数据库将应运而生在“SQL Server对象能源管理器”中“SQL连接”的“数据库”小节中。

EntityFramework
System.Data.Entity
System.ComponentModel.DataAnnotations //对Model层属性的概念,用属性注明来做持久化映射。
EF4.第11中学帮衬的牢笼如下属性:

尾声

迄今,整个多租户的架构就着力做到了。当然我们还是能实行增添,举个例子实现租户缓存、租户资源管理等等,那是三回九转的话题了。

13.2.2 Adding the Identity Packages 13.2.2 添加Identity包

Identity is published as a set of NuGet packages, which makes it easy to install them into any project. Enter the following commands into the Package Manager Console:

Identity是当做一组NuGet包发表的,因而轻易将其设置到任何项目。请在“Package Manager Console(包管理器调控台)”中输入以下命令:

Install-Package Microsoft.AspNet.Identity.EntityFramework –Version 2.0.0(2.2.1)
Install-Package Microsoft.AspNet.Identity.OWIN -Version 2.0.0(2.2.1)
Install-Package Microsoft.Owin.Host.SystemWeb -Version 2.1.0(3.0.1)

Visual Studio can create projects that are configured with a generic user account management configuration, using the Identity API. You can add the templates and code to a project by selecting the MVC template when creating the project and setting the Authentication option to Individual User Accounts. I don't use the templates because I find them too general and too verbose and because I like to have direct control over the contents and configuration of my projects. I recommend you do the same, not least because you will gain a better understanding of how important features work, but it can be interesting to look at the templates to see how common tasks are performed.
Visual Studio能够创设一些用到Identity API的档案的次序,这种类型以“泛型客户账号处理配置”举办布置。你能够给品种增进一些模板和代码,只需在创建项目时精选MVC模板,并将注解选项设置为Individual User Accounts(个别顾客账号)。作者并没有动用这种模板,因为本身发掘它们太普通,也太拉杂,并且小编爱不忍释对本身项目中的内容和布署有直接的垄断(monopoly)。作者提出您也这么做,那不单令你能够对首要特色的行事体制得到越来越好的知道,而且,侦查模板怎么样实行常规职分也许有趣的。

13.2.2 Adding the Identity Packages 13.2.2 添加Identity包

Identity is published as a set of NuGet packages, which makes it easy to install them into any project. Enter the following commands into the Package Manager Console:
Identity是用作一组NuGet包宣布的,因而轻易将其安装到别的类型。请在“Package Manager Console”中输入以下命令:

Install-Package Microsoft.AspNet.Identity.EntityFramework –Version 2.0.0Install-Package Microsoft.AspNet.Identity.OWIN -Version 2.0.0Install-Package Microsoft.Owin.Host.SystemWeb -Version 2.1.0

Visual Studio can create projects that are configured with a generic user account management configuration, using the Identity API. You can add the templates and code to a project by selecting the MVC template when creating the project and setting the Authentication option to Individual User Accounts. I don't use the templates because I find them too general and too verbose and because I like to have direct control over the contents and configuration of my projects. I recommend you do the same, not least because you will gain a better understanding of how important features work, but it can be interesting to look at the templates to see how common tasks are performed.
Visual Studio能够创立一些使用Identity API的种类,那种类型以“泛型顾客账号管理配置”举办布署。你能够给品种增加一些模板和代码,只需在创制项目时选用MVC模板,并将表明选项设置为Individual User Accounts。小编从不利用这种模板,因为小编意识它们太普通,也太拉杂,何况笔者爱好对我项目中的内容和配备有直接的调节。作者提出您也如此做,那不但令你能够对根本特征的做事机制得到更加好的知情,而且,考察模板如何进行例行任务也是幽默的。

Key 主键
StringLength 字符串长度
MaxLength 最大尺寸
ConcurrencyCheck
Required 必需
Timestamp 时间戳
ComplexType 复合类型
Column 映射列:column name, ordinal & data type
Table 映射表:name 和schema
InverseProperty
ForeignKey 外键
DatabaseGenerated
NotMapped 在数据库中消除
3、编写Model类

13.2.3 Updating the Web.config File 13.2.3 更新Web.config文件

Two changes are required to the Web.config file to prepare a project for ASP.NET Identity. The first is a connection string that describes the database I created in the previous section. The second change is to define an application setting that names the class that initializes OWIN middleware and that is used to configure Identity. Listing 13-4 shows the changes I made to the Web.config file. (I explained how connection strings and application settings work in Chapter 9.)
为了搞活项目选拔ASP.NET Identity的备选,必要在Web.config文件中做两处更动。第一处是接连字符串,它呈报了本人在上一小节中创立的数据库。第二处改换是概念八个应用程序设置,它命名对OWIN中间件实行开端化的类,并将它用来配置Identity。清单展现了对Web.config文件的修改(第9章早就表明过延续字符串和应用程序设置)。

Listing 13-4. Preparing the Web.config File for ASP.NET Identity
清单13-4. 为ASP.NET Identity准备Web.config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework"
            type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
            EntityFramework, Version=6.0.0.0, Culture=neutral,
            PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections> 

    <connectionStrings>
        <add name="IdentityDb" providerName="System.Data.SqlClient"
            connectionString="Data Source=(localdb)v11.0;
                Initial Catalog=IdentityDb;
                Integrated Security=True;
                Connect Timeout=15;
                Encrypt=False;TrustServerCertificate=False;
                MultipleActiveResultSets=True"/>
    </connectionStrings> 

    <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
        <add key="owin:AppStartup" value="Users.IdentityConfig" /> 
    </appSettings>
    <system.web>
        <compilation debug="true" targetFramework="4.5.1" />
        <httpRuntime targetFramework="4.5.1" />
    </system.web>
    <entityFramework>
        <defaultConnectionFactory
            type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,
            EntityFramework">
        <parameters>
            <parameter value="v11.0" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="System.Data.SqlClient"
            type="System.Data.Entity.SqlServer.SqlProviderServices,
            EntityFramework.SqlServer" />
        </providers>
    </entityFramework>
</configuration>

Caution Make sure you put the connectionString value on a single line. I had to break it over several lines to make the listing fit on the page, but ASP.NET expects a single, unbroken string. If in doubt, download the source code that accompanies this book, which is freely available from www.apress.com.
警告:要力保将connectionString的值放在一行中,这里把它给断开了,是为着让清单适应页面,但ASP.NET要求是十足无断行的字符串。假使有疑问,请下载本书的陪伴代码,下载地址:www.apress.com。

OWIN defines its own application startup model, which is separate from the global application class that I described in Chapter 3. The application setting, called owin:AppStartup, specifies a class that OWIN will instantiate when the application starts in order to receive its configuration.

OWIN定义了它和煦的应用程序运营模型,与第3章所描述的大局应用程序类是分开的。上述的应用程序设置,名称叫owin:AppStartup,钦定了应用程序运维时,将由OWIN进行实例化的类,指标是承受它的配备。

Tip Notice that I have set the MultipleActiveResultSets property to true in the connection string. This allows the results from multiple queries to be read simultaneously, which I rely on in Chapter 14 when I show you how to authorize access to action methods based on role membership.
提示:留神,作者以往在接连字符串团长MultipleActiveResultSets属性(多活动结果集)设置为true,那样能够由此而且读取的八个查询来造成结果,第14章便依据于这种方式,那时会亲自去做如何依照剧中人物成员来授权访谈动作方法。

13.2.3 Updating the Web.config File 13.2.3 更新Web.config文件

Two changes are required to the Web.config file to prepare a project for ASP.NET Identity. The first is a connection string that describes the database I created in the previous section. The second change is to define an application setting that names the class that initializes OWIN middleware and that is used to configure Identity. Listing 13-4 shows the changes I made to the Web.config file. (I explained how connection strings and application settings work in Chapter 9.)
为了搞活项目采纳ASP.NET Identity的预备,必要在Web.config文件中做两处改动。第一处是接连字符串,它陈诉了自身在上一小节中创制的数据库。第二处改造是概念一个应用程序设置,它取名对OWIN中间件进行开端化的类,并将它用来配置Identity。清单展现了对Web.config文件的修改(第9章早就表达过三番两次字符串和应用程序设置)。

Listing 13-4. Preparing the Web.config File for ASP.NET Identity
清单13-4. 为ASP.NET Identity准备Web.config文件

<?xml version="1.0" encoding="utf-8"?><configuration>    <configSections>        <section name="entityFramework"            type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,            EntityFramework, Version=6.0.0.0, Culture=neutral,            PublicKeyToken=b77a5c561934e089" requirePermission="false" />    </configSections> 
    <connectionStrings>        <add name="IdentityDb" providerName="System.Data.SqlClient"            connectionString="Data Source=v11.0;                Initial Catalog=IdentityDb;                Integrated Security=True;                Connect Timeout=15;                Encrypt=False;TrustServerCertificate=False;                MultipleActiveResultSets=True"/>    </connectionStrings> 
    <appSettings>        <add key="webpages:Version" value="3.0.0.0" />        <add key="webpages:Enabled" value="false" />        <add key="ClientValidationEnabled" value="true" />        <add key="UnobtrusiveJavaScriptEnabled" value="true" />        <add key="owin:AppStartup" value="Users.IdentityConfig" />     </appSettings>    <system.web>        <compilation debug="true" targetFramework="4.5.1" />        <httpRuntime targetFramework="4.5.1" />    </system.web>    <entityFramework>        <defaultConnectionFactory            type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,            EntityFramework">        <parameters>            <parameter value="v11.0" />        </parameters>    </defaultConnectionFactory>    <providers>        <provider invariantName="System.Data.SqlClient"            type="System.Data.Entity.SqlServer.SqlProviderServices,            EntityFramework.SqlServer" />        </providers>    </entityFramework></configuration>

Caution Make sure you put the connectionString value on a single line. I had to break it over several lines to make the listing fit on the page, but ASP.NET expects a single, unbroken string. If in doubt, download the source code that accompanies this book, which is freely available from www.apress.com.
警告:要力保将connectionString的值放在一行中,这里把它给断开了,是为着让清单适应页面,但ASP.NET必要是纯净无断行的字符串。要是有疑点,请下载本书的伴随代码,下载地址:www.apress.com。

OWIN defines its own application startup model, which is separate from the global application class that I described in Chapter 3. The application setting, called owin:AppStartup, specifies a class that OWIN will instantiate when the application starts in order to receive its configuration.
OWIN定义了它协和的应用程序运营模型,与第3章所描述的大局应用程序类是分开的。上述的应用程序设置,名字为owin:AppStartup,内定了应用程序运维时,将由OWIN进行实例化的类,目标是接受它的布署。

Tip Notice that I have set the MultipleActiveResultSets property to true in the connection string. This allows the results from multiple queries to be read simultaneously, which I rely on in Chapter 14 when I show you how to authorize access to action methods based on role membership.
提示:瞩目,笔者已经在延续字符串准将MultipleActiveResultSets属性设置为true,那样能够由此而且读取的多个查询来产生结果,第14章便借助于这种措施,那时会事必躬亲如何依照剧中人物成员来授权访谈动作方法。

using System.ComponentModel.DataAnnotations.dll

本文由1010cc时时彩标准版发布于三分时时彩1010CC,转载请注明出处:1010cc时时彩标准版:多租户的设计与实现,Iden

关键词:

上一篇:【1010cc时时彩标准版】php获取数据库结果集方法

下一篇:没有了