简体   繁体   English

在现有的Web应用程序中实现ASP.NET Identity

[英]Implementing ASP.NET Identity into existing web application

Having read lots of articles and making lots of false starts I need some advise on adding Microsoft ASP.NET Identity to my existing ASP.NET Web Forms application. 看了很多 文章 ,使许多错误的开始,我需要在加入微软ASP.NET身份 ,以我现有的ASP.NET Web窗体应用一些建议的。 There are so many subtle differences between my existing application and typical samples out there that I don't know how to get off the ground. 我现有的应用程序与典型的示例之间有许多细微的差别,我不知道该如何起步。 Here are some points about my existing application: 以下是有关我现有应用程序的一些要点:

  1. It is a ASP.NET Web Forms application (.NET Framework 4.6) that has evolved over about 4-5 years 它是经过大约4-5年的发展的ASP.NET Web窗体应用程序(.NET Framework 4.6)
  2. It uses SQL Server database though Entity Framework (not Code First but Database First). 它通过实体框架(不是代码优先,而是数据库优先)使用SQL Server数据库。 The database pre-dates the web application and is based on 10 year old designs. 该数据库是Web应用程序之前的版本,并且基于10年的设计。 It does however have good table relationships etc. 但是它确实具有良好的表关系等。
  3. My database's schema for Users and Roles are not structured as per "ASP.NET Identity / EntityFramework". 我的用户和角色数据库结构未按照“ ASP.NET身份/ EntityFramework”进行结构化。 It has a [User] table with ID int PK , no username (as such), Password, First Name, Surname, Email and some other properties. 它有一个[用户]表,其ID int PK ,没有用户名(例如),密码,名字,姓氏,电子邮件和其他一些属性。 It also has a Roles table (4-5 entries) and a UserRoles table that provides many-many relationships. 它还具有一个角色表(4-5个条目)和一个提供许多关系的UserRoles表。 There is no "claims" as such other than properties on the User table. 除了“用户”表上的属性外,没有其他“声明”。
  4. Existing login is based on entering a user id (primary key) and password which is then checked against the user table and on successful login (stored proc), details are then stored in Session - something that in itself isn't quite right! 现有的登录基于输入用户ID(主键)和密码,然后根据用户表和成功登录(存储的proc)进行检查,然后将详细信息存储在Session中-这本身并不完全正确!
  5. Although using Entity Framework, the Connection String is formed at runtime based on a template connection string in web.config (basically, the database name and server instance name is populated dynamically) 尽管使用Entity Framework,但连接字符串是在运行时基于web.config的模板连接字符串形成的(基本上,数据库名称和服务器实例名称是动态填充的)

So, here are some the barriers, issues, questions I've come up with. 因此,这里有一些我遇到的障碍,问题和问题。 Any advise on any of the questions below would be most appreciated: 对于以下任何问题的任何建议,将不胜感激:

  1. Should I use as "User.Username" when my web application doesn't have one? 如果我的Web应用程序没有一个,我应该用作“ User.Username”吗? Should I use Email Address or UserID? 我应该使用电子邮件地址还是用户名? Email address unfortunately isn't unique across the existing [User] data set. 不幸的是,电子邮件地址在现有的[User]数据集中并不是唯一的。 Should I use a string version of my (int) UserID? 我应该使用(int)UserID的字符串版本吗?
  2. Should I allow ASP.NET Identity to set up its own code first tables into my own database and then add a mapping to my legacy [User] table? 我是否应该允许ASP.NET Identity首先在自己的数据库中设置自己的代码表,然后将映射添加到旧的[User]表?
  3. How can I inject a "resolver" to provide a (dynamically generated) connection string to the existing ASP.NET Identity Data Stored/Data Access Layer? 如何注入“解析器”以提供(动态生成的)连接字符串到现有的ASP.NET身份数据存储/数据访问层?
  4. How much of the Store classes am I likely needing to replace? 我可能需要替换多少个商店类?
  5. I've read that NOT using an ORM/EntityFramework can provide very inefficient data access. 我已经读过,不使用ORM / EntityFramework会提供非常低效的数据访问。 Does this mean that if implementing my own Storage classes, I can't simply use some of my existing Stored Procedures? 这是否意味着如果实现自己的存储类,就不能简单地使用一些现有的存​​储过程?
  6. My Web Forms application isn't making use of any async code. 我的Web窗体应用程序未使用任何async代码。 Is this going to cause me problems with the ASP.NET Identity framework? 这会给我带来ASP.NET Identity框架问题吗? I've heard that async works best if implemented right through the call stack. 我听说, async最好在调用堆栈中实现。
  7. Do I need to somehow disable the existing Forms Based Authentication? 我是否需要以某种方式禁用现有的基于表单的身份验证? Are they two going to clash over writing to HttpContext.User for example? 例如,他们两个会在写HttpContext.User发生冲突吗?

My Schema 我的架构

CREATE TABLE [dbo].[User](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [OrganisationID] [int] NOT NULL,
    [SaltHashPassword] [nvarchar](128) NOT NULL,
    [FirstName] [nvarchar](64) NOT NULL,
    [Surname] [nvarchar](64) NOT NULL,
    [Email] [nvarchar](128) NULL,
    --...
 CONSTRAINT [PK_User] PRIMARY KEY NONCLUSTERED ([ID] ASC)
) ON [PRIMARY]

CREATE TABLE [dbo].[Roles](
    [Role] [varchar](8) NOT NULL,
    [Name] [varchar](32) NOT NULL,
    [Description] [varchar](1000) NOT NULL,
    [OrderNo] [tinyint] NOT NULL,
 CONSTRAINT [PK_Roles] PRIMARY KEY CLUSTERED ([Role] ASC)--...
 CONSTRAINT [IX_Roles] UNIQUE NONCLUSTERED ([Role] ASC)--...
) ON [PRIMARY]

CREATE TABLE [dbo].[UserRole](
    [UserID] [int] NOT NULL,
    [Role] [varchar](8) NOT NULL,
 CONSTRAINT [PK_UserRole] PRIMARY KEY CLUSTERED ([UserID] ASC,[Role] ASC)
) ON [PRIMARY]

That's a lot of questions here. 这里有很多问题。 I think a good starting point is this migration article: Migrating an Existing Website from SQL Membership to ASP.NET Identity 我认为这篇迁移文章是一个很好的起点: 将现有网站从SQL成员身份迁移到ASP.NET Identity

It will probably help you answer most of your questions here, especially 1, 3 and 4. 它可能会帮助您在此处回答大部分问题,尤其是1、3和4。

Answer to Question 2: As ASP.NET Membership and ASP.NET Identity are two different things, I would suggest a full migration, meaning that once you created the new tables for Identity, you map your foreign keys to the new tables and get rid of ASP.NET Membership tables. 问题2的答案:由于ASP.NET Membership和ASP.NET Identity是两件不同的事情,因此我建议进行完全迁移,这意味着一旦为Identity创建了新表,就将外键映射到新表并摆脱掉ASP.NET成员资格表。

Answer to Question 5: I think it depends how you want to set up ASP.NET Identity. 对问题5的回答:我认为这取决于您要如何设置ASP.NET身份。 In the project I am currently working on, we decided not to use Entity Framework at all, but this makes everything more complex as you have to get very deep knowledge of what happens, when, how, why... So if you are looking for easy implementation, use EF it makes life much more easy and you will find plenty of code samples on Internet. 在我目前正在从事的项目中,我们决定完全不使用Entity Framework,但这会使一切变得更加复杂,因为您必须非常深入地了解发生的情况,时间,方式,原因...因此,如果您正在寻找为了易于实现,请使用EF,它使工作变得更加轻松,并且您会在Internet上找到大量的代码示例。

Answer to Question 6: If you don't use async, it won't cause issues. 对问题6的回答:如果不使用异步,它将不会引起问题。 The only downside is that you won't benefit from it... 唯一的缺点是您不会从中受益...

Answer to Question 7: Microsoft does not recommend having multiple authentication systems activated. 对问题7的回答:Microsoft不建议激活多个身份验证系统。 In the project I am currently working on, we managed to have both Identity and Azure AD authentication working together, but we had to develop our own middlewares and it was not that easy. 在我当前正在从事的项目中,我们设法使Identity和Azure AD身份验证一起工作,但是我们必须开发自己的中间件,这并不容易。 In our web.config, we have: 在我们的web.config中,我们有:

<system.web>
    ...
    <authentication mode="None" />
    ...
</system.web>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM