2013-08-05 7 views
5

Windows認証の仕組みとその実装方法を理解しようとしています。私はかなりの記事を読んで、かなり長さの動画をYouTubeで見ていましたが、私は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 

そして、ここでは、Windows認証とSQL認証を混乱されている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

'authorization'要素を使って場所をロックダウンする必要があります。 –

+0

ユーザー名とパスワードのデータベースに対する何らかの検査をしています。これはフォームベースの認証の多くです。 Windows認証の目的はそれを持たないことです。あるいは、Windows認証が必要な場合、認証はログインページにアクセスできるユーザーをロックダウンします。 web.configに認証要素を正しく配置しましたが、認証要素がありません。このページを参照して理解してください。 http://msdn.microsoft.com/en-us/library/8d82143t(v=vs.85).aspx – Bearcat9425

+2

web.configファイルにこの行を追加しました。 index.aspxページに追加する必要があるコードはありますか? –

答えて

6

です。

Windows認証に基づいて動作するように、このWebページのためには、あなたのweb.configファイルは、Webサーバーにページを展開するとき、あなたは外部のユーザーを制限する匿名認証を無効にする必要があり

<authentication mode="Windows"> 

を必要とします。以下はIIS7 +ウェブサーバの認証部からの抜粋です:

enter image description here

enter image description here

ユーザーまたはそのグループにログインに対してプログラミングする必要がある場合は、あなたがWindowsIdentityクラスを使用する必要があります。

+1

私はセキュリティ問題を遭遇したのは初めてですが、それを直ちにやろうとしていますが、混乱しています。ありがとう –

関連する問題