2013-08-05 32 views
5

Próbuję zrozumieć, jak działa uwierzytelnianie systemu Windows i jak go wdrożyć. Przeczytałem sporo artykułów i obejrzałem trochę filmów na youtube, ale wciąż nie mogę zrozumieć, co należy dodać do mojej strony web.config file/index.aspx, aby działało poprawnie.Korzystanie z uwierzytelniania systemu Windows w asp.net z C#

Oto strona 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 

A oto plik 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

Musisz zablokować lokalizacje za pomocą elementów 'authorization'. –

+0

Dokonujesz pewnego rodzaju sprawdzenia w bazie danych pod kątem nazwy użytkownika i hasła, które jest bardziej oparte na formularzach uwierzytelniania. Celem uwierzytelniania systemu Windows jest to, że go nie ma, lub jeśli potrzebujesz, aby uwierzytelnianie systemu Windows blokowało dostęp do strony logowania. Poprawnie umieściłeś w web.config element uwierzytelniający, ale brakuje Ci elementu autoryzacji. Zobacz tę stronę i omówienie. http://msdn.microsoft.com/en-us/library/8d82143t(v=vs.85).aspx – Bearcat9425

+2

Dzięki chłopaki, dodałem tę linię do pliku web.config Czy istnieje dodatkowy kod, który powinienem dodać do strony index.aspx? –

Odpowiedz

6

Mylisz uwierzytelniania SQL z uwierzytelniania systemu Windows.

Aby tej strony do pracy w oparciu o uwierzytelnianie systemu Windows, web.config potrzebuje

<authentication mode="Windows"> 

Podczas wdrażania swoją stronę na serwer WWW, trzeba wyłączyć uwierzytelnianie anonimowe, aby ograniczyć użytkownikom zewnętrznym. Poniżej znajduje się fragment z sekcji uwierzytelniania serwera WWW IIS7 + za:

enter image description here

enter image description here

Jeśli trzeba zaprogramować przeciwko zalogowanego użytkownika lub jego grupy, trzeba użyć WindowsIdentity klasie.

+1

Widzę, to pierwszy raz, gdy napotkałem problemy bezpieczeństwa, więc próbuję to zrobić dobrze, ale się mylić, dziękuję –

Powiązane problemy