2013-08-05 1 views
5

어떻게 Windows 인증이 작동하는지 이해하고 구현하는 방법을 이해하려고합니다. 필자는 꽤 많은 기사를 읽고 유튜브에서 아주 짧은 길이의 비디오를 보았지만 제대로 작동하려면 내 web.config 파일/index.aspx 페이지에 추가해야 할 내용을 고집하지 않아도됩니다. 여기 asp.net에서 windows 인증을 사용하여 C#

는 index.aspx 페이지입니다 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Data; 

namespace asset_management_system 
{ 
    public partial class index1 : System.Web.UI.Page 
    { 

    DataAccessLayer dal = new DataAccessLayer(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void loginBut_Click(object sender, EventArgs e) 
    { 

     string username = usernameTB.Text.Trim(); 
     string password = passwordTB.Text.Trim(); 

     try 
     { 
      using (SqlDataReader dr = dal.CheckLoginDetails(username)) 
      { 
       //if username does not exist 
       if (!dr.Read()) 
       { 
        MessageBox.Show("Invalid login details"); 
       } 

       else 
       { 
        //if password matches the username then redirect to home page 
        if (dr[0].ToString() == password) 
        { 
         Session["username"] = username; 
         Response.Redirect("Home/home.aspx"); 
        } 
        else 
        { 
         MessageBox.Show("Invalid login details"); 
        } 
       } 
      } 
     } 
     catch (SqlException sqlex) { MessageBox.Show("There may be an issue with the server, please contact the administrator" + 
                " and provide this error message: " + sqlex); } 
     catch (Exception ex) { MessageBox.Show("error message: " + ex); } 


    }//end of loginBut_click method 


    }//end of class 
}//end of namespace 

그리고 여기 web.config 파일

<?xml version="1.0"?> 

<configuration> 

    <connectionStrings> 
    <add name="Asset management System DBConnectionString" connectionString="Data Source=STEPHENP\SQLEXPRESS;Initial Catalog=&quot;Asset management System DB&quot;;Integrated Security=True" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 

    <system.web> 

    <compilation debug="true" targetFramework="4.0"> 
     <assemblies> 
     <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 
     <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
     <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 
     </assemblies> 
    </compilation> 

    <authentication mode="Windows"> 
    </authentication> 
    <identity impersonate="true"/> 

    </system.web> 

</configuration> 
+0

'인증'요소를 사용하여 위치를 잠글 필요가 있습니다. –

+0

당신은 데이터베이스에 대해 사용자 이름과 암호를 검사하고 있습니다. 그보다 폼 기반 인증에 가깝습니다. Windows 인증의 목적은 그럴 필요가 없거나 Windows가 필요한 경우 인증은 로그인 페이지에 액세스 할 수있는 사람을 잠급니다. web.config에 authentication 요소를 올바르게 배치했지만 인증 요소가 누락되었습니다. 이 페이지 및 이해를 참조하십시오. http://msdn.microsoft.com/en-us/library/8d82143t(v=vs.85).aspx – Bearcat9425

+2

감사합니다.이 줄을 web.config 파일에 추가했습니다. 내 index.aspx 페이지에 추가해야하는 코드가 있습니까? –

답변

6

당신은 Windows 인증과 SQL 인증을 혼동이다. 이 웹 페이지 위해서는

은 Windows 인증을 기반으로하려면, 당신의 Web.config 당신이 웹 서버에 페이지를 배포 할 때

<authentication mode="Windows"> 

이 필요합니다, 당신은 외부 사용자를 제한하는 익명 인증을 사용하지 않도록해야합니다.

enter image description here

당신이 사용자 또는 그룹에 로그인 대해 프로그래밍해야하는 경우

enter image description here

, 당신은 WindowsIdentity 클래스를 사용할 필요가 : 아래는 IIS7 + 웹 서버의 인증 섹션에서 조각입니다.

+1

나는 처음에는 보안 문제를 겪었으므로 지금 당장은 그것을 혼란스럽게하려고 노력하고있다. 감사하다. –