简体   繁体   English

EF Code First如何正确设置数据库凭据

[英]EF Code First how correctly set up database credentials

I have developer MVC4 + EF Code First + SQL Server 2008 web app. 我有开发人员MVC4 + EF Code First + SQL Server 2008网络应用程序。 Uploaded it to prod server with IIS7. 使用IIS7将其上载到prod服务器。 Created new credentials PC . 创建了新的凭证PC Added empty database PCDB to SQL Server and assigned user PC to it with owner permission. 向SQL Server添加了空数据库PCDB ,并在拥有者权限的情况下为其分配了用户PC。 When I run web app I get error 当我运行Web应用程序时,我收到错误

Model compatibility cannot be checked because the database does not contain model metadata. 无法检查模型兼容性,因为数据库不包含模型元数据。 Model compatibility can only be checked for databases created using Code First or Code First Migrations 只能检查使用Code First或Code First Migrations创建的数据库的模型兼容性

My connection string is 我的连接字符串是

data source=174.xx.x.x;initial catalog=pcdb;user id=pc;password=xxxxx;

The exception is understandable, I can delete PCDB database and let EFCode First create it by itself. 例外是可以理解的,我可以删除PCDB数据库并让EFCode首先自己创建它。 But how about credentials PC ? 但凭证PC怎么样? I do not want to make PC user as administrator but without it EF Code First will not be able to create new database in SQL Server. 我不想让PC用户成为管理员,但没有它,EF Code First将无法在SQL Server中创建新数据库。

How to solve the problem? 如何解决问题?

The overall design starts with Forms or Windows authentication at the WebsiteASP.NET/ IIS. 整体设计从WebsiteASP.NET / IIS的Forms或Windows身份验证开始。 and ends with Application and DB authentication you want/need. 并以您想要/需要的应用程序和数据库身份验证结束。 Application authorization is another topic. 应用程序授权是另一个主题 I will not discuss that here. 我不会在这里讨论。

You dont actually state the authentication model desired. 您实际上并未说明所需的身份验证模型。 So I will start with a disclaimer. 所以我将从免责声明开始。 This a suggestion that I WOULD use in a production site. 这是我将在生产现场使用的建议。 But it is not the ultimate end game nor is it the ONLY short term solution you might consider. 但它不是最终的终极游戏,也不是您可能考虑的唯一短期解决方案。

This is a solution that a one man show can get working . 这是一个单人表演可以开始工作的解决方案 And is secure and without excessive admin effort to keep running. 并且是安全的,没有过多的管理工作来继续运行。

Use SQL server logon via Windows Auth BUT you do not need to add every user to SQL server. 通过Windows身份验证使用SQL Server登录但是您不需要将每个用户添加到SQL Server。
There is also the option of impersonation. 还有冒充的选择。 But that can get tricky and this explanation is not NOT impersonation. 但这可能会变得棘手,这种解释并非模仿。 That is another another approach. 这是另一种方法。

first make sure Website is using Windows Authentication 首先确保网站使用Windows身份验证

set IIS to use Windows Authentication: 设置IIS以使用Windows身份验证: 在此输入图像描述

Now the APP Pool behind the website on IIS you have configured. 现在您已配置IIS的网站背后APP池 .

Im going to suggest a Psuedo-service user in the APP pool as a good way to start . 我打算在APP池中建议一个Psuedo服务用户作为一个好的开始方式 ie WEBAPPLICATION_X_USER. 即WEBAPPLICATION_X_USER。 You can have a separate user per APP pool. 每个APP池可以有一个单独的用户。 Each user can access only its DB. 每个用户只能访问其数据库。 So you get application separation. 所以你得到应用程序分离。 Your enter a user and password here. 您在此输入用户名和密码。 IIS will encrypt and decrypt as required. IIS将根据需要进行加密和解密。 (better than plan text in Web.config) (比Web.config中的计划文本更好)

在此输入图像描述 This user should have reduced auth on the server itself. 此用户应减少服务器本身的身份验证。 NOT AN ADMIN user on domain or even local admin. 不是域上的ADMIN用户,甚至是本地管理员。 Just enough so it can use Sql server to create a DB. 这足以让它可以使用Sql server来创建一个DB。 So create a regular windows user 因此,创建一个常规的Windows用户

Let ASP.Net logon to DB. 让ASP.Net登录到DB。 Let ASP.net encrypt and decrypt the password. 让ASP.net加密和解密密码。

SQL Server中的特殊服务用户权限

So now the situation is Windows AUTH on IIS. 所以现在的情况是IIS上的Windows AUTH。 IIS has an App pool with a special windows user that can logon to SQL server. IIS有一个带有特殊Windows用户的应用程序池,可以登录到SQL服务器。 You have added this user to SQL server instance and Allocated this service user the ability create DBs. 您已将此用户添加到SQL Server实例,并为此服务用户分配了创建DB的功能。 Dont give the user access to ALL Dbs :-) Just the one it will create. 不要让用户访问所有Dbs :-)只是它将创建的那个。 Plus public access (via EF). 加上公共访问(通过EF)。

Verify the user credential situation in your WEB APP. 验证WEB APP中的用户凭据情况。 See [System.Security.Principal.WindowsIdentity] This should show your windows authenticated end user. 请参阅[System.Security.Principal.WindowsIdentity]这应该显示您的Windows身份验证的最终用户。

System.Environment.UserName should have the service user ID you placed in the IIS APP POOL. System.Environment.UserName应具有您在IIS APP POOL中放置的服务用户标识。

Now when EF goes to create or access data on the SQL server instance, it will connect with System.Environment.UserName if the WEB.CONFIG entry is set to use windows integrated security 现在当EF去创建或访问SQL服务器实例上的数据时,如果WEB.CONFIG条目设置为使用windows集成安全性,它将与System.Environment.UserName连接

<connectionStrings>
<add name="DbContextName" connectionString="Data Source=Your SQL server Instance;Initial Catalog=The DBNAME;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

And you KNOW the authenticated user . 而且你知道经过身份验证的用户 httpContext will give it to you as does thread current principal . httpContext会像线程当前主体一样给你。 HttpContext.User is by default mapped to {System.Security.Principal.WindowsPrincipal} HttpContext.User默认映射到{System.Security.Principal.WindowsPrincipal}

So you can perform application level checking. 所以你可以执行应用程序级别检查。 The same approach should also work with Forms Authentication . 相同的方法也适用于表单身份验证

WARNING: If you have windows WPF approach (ie you are not using IIS and therefore no APP pool), then this approach MUST be changed and is more complex and no longer the best place to start. 警告:如果你有Windows WPF方法(即你没有使用IIS,因此没有APP池),那么这种方法必须改变,并且更复杂,不再是最好的起点。

I hope this helps you get started 我希望这可以帮助你开始

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

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