Chapter 1__1.6 Connecting to SQL Server Using Integrated Security from ASP.NET(集成安全连接数据库)

<p>How do it :</p> <p>(1)创建一个解决方案</p> <p>(2)在<span class="docEmphasis">Web.config配置文件里面的</span><tt>&lt;system.web&gt;节点里面添加</tt></p> <p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <div class="cnblogs_code"> <pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"><tt>&lt;</tt></span><span style="color: #000000;"><tt>authentication mode</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>Windows</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #000000;"><tt>/&gt;</tt></span><span style="color: #000000;"><tt><br /> </tt></span><span style="color: #000000;"><tt>&lt;</tt></span><span style="color: #000000;"><tt>identity impersonate</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>true</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #000000;"><tt>/&gt;</tt></span><span style="color: #000000;"><tt><br /></tt></span></div></pre> </div> </tt></p> <p><tt>(3)添加连接字符串:</tt></p> <p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <div class="cnblogs_code"> <pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"><tt>&lt;</tt></span><span style="color: #000000;"><tt>connectionStrings</tt></span><span style="color: #000000;"><tt>&gt;</tt></span><span style="color: #000000;"><tt><br /> </tt></span><span style="color: #000000;"><tt>&lt;</tt></span><span style="color: #000000;"><tt>add name</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>AdventureWorks</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt> providerName</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>System.Data.SqlClient</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt><br /> connectionString</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>Data Source=(local);</tt></span><span style="color: #800000;"><tt><br /></tt></span><span style="color: #000000;"><tt> Integrated security</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt>SSPI;Initial Catalog</tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt>AdventureWorks;</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>/&gt;</tt></span><span style="color: #800000;"><tt><br /></tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #000000;"><tt>&lt;/</tt></span><span style="color: #000000;"><tt>connectionStrings</tt></span><span style="color: #000000;"><tt>&gt;</tt></span></div></pre> </div> 实例</tt></p> <p><tt> <div class="cnblogs_code" onclick="cnblogs_code_show('9b83c0ae-8e84-4a18-b251-f9ebd5904af8')"><tt><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" class="code_img_closed" id="code_img_closed_9b83c0ae-8e84-4a18-b251-f9ebd5904af8" /><img src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" class="code_img_opened" id="code_img_opened_9b83c0ae-8e84-4a18-b251-f9ebd5904af8" onclick="cnblogs_code_hide('9b83c0ae-8e84-4a18-b251-f9ebd5904af8',event)" style="display: none;" /><span class="cnblogs_code_collapse">代码</span></tt> <div id="cnblogs_code_open_9b83c0ae-8e84-4a18-b251-f9ebd5904af8" class="cnblogs_code_hide"> <pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff;"><tt>using</tt></span><span style="color: #000000;"><tt> System;<br /></tt></span><span style="color: #0000ff;"><tt>using</tt></span><span style="color: #000000;"><tt> System.Data;<br /></tt></span><span style="color: #0000ff;"><tt>using</tt></span><span style="color: #000000;"><tt> System.Data.SqlClient;<br /></tt></span><span style="color: #0000ff;"><tt>using</tt></span><span style="color: #000000;"><tt> System.Configuration;<br /><br /></tt></span><span style="color: #0000ff;"><tt>namespace</tt></span><span style="color: #000000;"><tt> IntegratedSecurityFromAspNet<br />{<br /> </tt></span><span style="color: #0000ff;"><tt>public</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #0000ff;"><tt>partial</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #0000ff;"><tt>class</tt></span><span style="color: #000000;"><tt> _Default : System.Web.UI.Page<br /> {<br /> </tt></span><span style="color: #0000ff;"><tt>protected</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #0000ff;"><tt>void</tt></span><span style="color: #000000;"><tt> Page_Load(</tt></span><span style="color: #0000ff;"><tt>object</tt></span><span style="color: #000000;"><tt> sender, EventArgs e)<br /> {<br /> </tt></span><span style="color: #0000ff;"><tt>string</tt></span><span style="color: #000000;"><tt> sqlText </tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>SELECT TOP 10 * FROM Person.Contact</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt>;<br /> </tt></span><span style="color: #0000ff;"><tt>string</tt></span><span style="color: #000000;"><tt> connectString </tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt> <br /> ConfigurationManager.ConnectionStrings[<br /> </tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>AdventureWorks</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt>].ConnectionString;<br /> DataTable dt </tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #0000ff;"><tt>new</tt></span><span style="color: #000000;"><tt> DataTable( );<br /> SqlDataAdapter da </tt></span><span style="color: #000000;"><tt>=</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #0000ff;"><tt>new</tt></span><span style="color: #000000;"><tt> SqlDataAdapter(sqlText, connectString);<br /> da.Fill(dt);<br /><br /> </tt></span><span style="color: #0000ff;"><tt>foreach</tt></span><span style="color: #000000;"><tt> (DataRow row </tt></span><span style="color: #0000ff;"><tt>in</tt></span><span style="color: #000000;"><tt> dt.Rows)<br /> Response.Write(row[</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>ContactID</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt>] </tt></span><span style="color: #000000;"><tt>+</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt> - </tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #000000;"><tt>+</tt></span><span style="color: #000000;"><tt> row[</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>LastName</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt>] </tt></span><span style="color: #000000;"><tt>+</tt></span><span style="color: #000000;"><tt><br /> </tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>, </tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #000000;"><tt>+</tt></span><span style="color: #000000;"><tt> row[</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>FirstName</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt>] </tt></span><span style="color: #000000;"><tt>+</tt></span><span style="color: #000000;"><tt> </tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #800000;"><tt>&lt;br/&gt;</tt></span><span style="color: #800000;"><tt>"</tt></span><span style="color: #000000;"><tt>);<br /> }<br /> }<br />}<br /><br /> </tt></span></div></pre> </div> </div> 下面<br /></tt></p> <p>讨论两种验证。windows验证和sql验证(<span class="docPubcolor"><span class="docEmphasis">SQL Server Authentication</span></span>)</p> <p> <title>connecting_to_sql_server_using_integrate.html</title> <link href="http://www.cnblogs.com/MicrosoftChina/admin/images/css1.css" type="text/css" rel="STYLESHEET" /> <link href="http://www.cnblogs.com/MicrosoftChina/admin/images/css2.css" type="text/css" rel="STYLESHEET" /> </p> <div> <div><br /> <p class="docText"><a name="idx-CHP-1-0155"></a><a name="the same"></a>Integrated security requires that all application users are on the same domain so that their credentials are available to IIS. The following areas of the application need to be configured:<a name="idx-CHP-1-0156"></a></p> <ul> <li> <p class="docList">Configure the ASP.NET application so that <tt>Integrated Windows Authentication</tt> is enabled and <tt>Anonymous Access</tt> is disabled.</p> </li> <li> <p class="docList">The <span class="docEmphasis">web.config</span> file establishes the <a name="idx-CHP-1-0157"></a><a name="and that"></a>authentication mode that the application uses and that the application will run as or impersonate the user. Add the following elements to the <span class="docEmphasis">web.config</span> file within the <tt>&lt;system.web&gt;</tt> element:</p> <pre><div class="cnblogs_code"><pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">&lt;</span><span style="color: #000000;">authentication mode</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">Windows</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">/&gt;&lt;</span><span style="color: #000000;">identity impersonate</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">/&gt;</span></div></pre> </div> <p>&nbsp;</p> <br /></pre> </li> <li> <p class="docList"><a name="the SQL"></a>The connection string must contain attributes that tell the SQL Server that integrated security is used. Use the <tt>Integrated Security=SSPI</tt><a name="instead of"></a> attribute-and-value pair instead of the <tt>User ID</tt> and <tt>Password</tt> attributes in the connection string. The older attribute-and-value pair <tt>Trusted_Connection=Yes</tt> is also supported.</p> </li> <li> <p class="docList"><a name="access permissions"></a>Add users and groups from the domain and set their access permissions as required.</p> </li> </ul> <p class="docText"><a name="By default"></a>By default, <a name="idx-CHP-1-0158"></a><a name="in the"></a>ASP.NET applications run in the context of a local user <tt>ASPNET</tt><a name="The account"></a> on IIS. The account has limited permissions and is local to the IIS computer and therefore not recognized as a user on remote computers. To use SQL Server on a different computer than IIS, run the web application in the context of a domain user recognized on both IIS and SQL Server computers.</p> <p class="docText"><a name="SQL Server"></a>In addition to the areas identified where IIS and SQL Server are on the same computer, the following additional items must be configured if the SQL Server is on a different domain:</p> <ul> <li> <p class="docList">Ensure that the mapped <a name="idx-CHP-1-0159"></a><a name="to run"></a>domain user has required privileges to run the <a name="idx-CHP-1-0160"></a>web application.</p> </li> <li> <p class="docList">Configure the <a name="idx-CHP-1-0161"></a><a name="elements to"></a>web application to impersonate the domain user. Add the following elements to the <span class="docEmphasis">web.config</span> file for the web application:</p> <pre><div class="cnblogs_code"><pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">&lt;</span><span style="color: #000000;">authentication mode</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">Windows</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">/&gt;</span><span style="color: #000000;"><br /> </span><span style="color: #000000;">&lt;</span><span style="color: #000000;">identity impersonate</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span><span style="color: #000000;"> userName</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">domainusername</span><span style="color: #800000;">"</span><span style="color: #000000;"><br /> password</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">myPassword</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">/&gt;</span><span style="color: #000000;"><br /></span></div></pre> </div> <p>&nbsp;</p> <br /></pre> </li> </ul> </div> </div> <table style="width: 100%;" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td align="right"> <div style="margin-left: 0.15in;"><a href="http://www.cnblogs.com/MicrosoftChina/admin/connecting_to_sql_server_using_an_ip_add.html"><img alt="Previous Page" src="http://www.cnblogs.com/MicrosoftChina/admin/images/prev.gif" align="absMiddle" border="0" height="20" width="20" /></a> <a href="http://www.cnblogs.com/MicrosoftChina/admin/connecting_to_an_oracle_database.html"><img alt="Next Page" src="http://www.cnblogs.com/MicrosoftChina/admin/images/next.gif" align="absMiddle" border="0" height="20" width="20" /></a> </div> </td> </tr> </tbody> </table>

你可能感兴趣的:(SQL Server)