2013-04-17 5 views
4

내 비주얼 스튜디오 I는 로그인 양식, 사용자가 넣을 수 있습니다 만들 지금 꿔 2012윈도우 윈폼 응용 프로그램

를에 C# 및 .NET 프레임 워크 4.5과 윈도우 폼 응용 프로그램을 만드는거야 대한 인증 양식 일부 사용자 이름과 비밀번호 (이전에 데이터베이스에서 생성) 및 애플리케이션이 사용자의 유효성을 검사하고 로그인합니다. 가능하다면 "Roles Control"을 사용하십시오.

Google에서 검색하려고하지만 ASP.NET에서 Windows Forms와 관련된이 콘텐츠를 찾지 못했습니다.

.NET Framework에는 WinForms의 인증 문제를 해결하기위한 좋은 (공식적인) 솔루션이 있습니까?

답변

3

아니요. 멤버십 시스템은 Asp.net의 일부이며, winforms 응용 프로그램에서 사용할 수는 있지만 매우 깨끗한 것은 아닙니다.

데이터베이스에 이미 사용자 이름과 암호가 있다면 코드 리버스 엔지니어링에 대해 걱정하지 않는 한 최선의 방법은 바로 인증 시스템으로 구현하는 것입니다 ...이 경우에는 멀리있는 것입니다 리버스 엔지니어링에 대비 한보다 진보 된 것.

편집 :

마이크로 소프트가 Windows Identity Foundation을 가지고 있지만, 당신은 아마도 원하는 것보다 정말 더 복잡한 시스템이다.

+0

나는 이해합니다. 나는 사용자 데이터베이스를 만들었습니다. 이 데이터의 유효성 검사를 만들고 사용자가 존재하고 암호가 맞으면 대시 보드 양식을 표시합니다. 어떻게 생각해? 답장을 보내 주셔서 감사합니다. 그리고 내 괴물 영어 미안해. :) –

1

나는 보통 다음과 같이 새로운 형식을 만듭니다.

public partial class LoginForm : Form 
{ 
    public bool letsGO = false; 

    public LoginForm() 
    { 
     InitializeComponent(); 
     textUser.CharacterCasing = CharacterCasing.Upper; 
    } 

    public string UserName 
    { 
     get 
     { 
      return textUser.Text; 
     } 
    } 

    private static DataTable LookupUser(string Username) 
    { 
     const string connStr = "Server=(local);" + 
          "Database=LabelPrinter;" + 
          "trusted_connection= true;" + 
          "integrated security= true;" + 
          "Connect Timeout=1000;"; 

     //"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;"; 

     const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName"; 
     DataTable result = new DataTable(); 
     using (SqlConnection conn = new SqlConnection(connStr)) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand(query, conn)) 
      { 
       cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username; 
       using (SqlDataReader dr = cmd.ExecuteReader()) 
       { 
        result.Load(dr); 
       } 
      } 
     } 
     return result; 
    } 

    private void HoldButton() 
    { 
     if (string.IsNullOrEmpty(textUser.Text)) 
     { 
      //Focus box before showing a message 
      textUser.Focus(); 
      MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 
      //Focus again afterwards, sometimes people double click message boxes and select another control accidentally 
      textUser.Focus(); 
      textPass.Clear(); 
      return; 
     } 
     else if (string.IsNullOrEmpty(textPass.Text)) 
     { 
      textPass.Focus(); 
      MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 
      textPass.Focus(); 
      return; 

     } 
     //OK they enter a user and pass, lets see if they can authenticate 
     using (DataTable dt = LookupUser(textUser.Text)) 
     { 
      if (dt.Rows.Count == 0) 
      { 
       textUser.Focus(); 
       MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
       textUser.Focus(); 
       textUser.Clear(); 
       textPass.Clear(); 
       return; 
      } 
      else 
      { 
       string dbPassword = Convert.ToString(dt.Rows[0]["Password"]); 
       string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB 

       Console.WriteLine(string.Compare(dbPassword, appPassword)); 

       if (string.Compare(dbPassword, appPassword) == 0) 
       { 
        DialogResult = DialogResult.OK; 
        this.Close(); 
       } 
       else 
       { 
        //You may want to use the same error message so they can't tell which field they got wrong 
        textPass.Focus(); 
        MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); 
        textPass.Focus(); 
        textPass.Clear(); 
        return; 
       } 
      } 
     } 
    } 

    private void textPass_KeyDown_1(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Return) 
     { 
      HoldButton(); 
     } 
    } 

    private void buttonLogin_Click(object sender, EventArgs e) 
    { 
     HoldButton(); 
    } 

    private void textPass_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Return) 
     { 
      HoldButton(); 
     } 
    } 
} 

다음 mainform에서이 작업을 수행 :

public Form1(string userName) 
{ 
    //this is incase a user has a particular setting in your form 
    //so pass name into contructer 
} 

을 한 후 마지막 : 나는 훨씬 그들의 확신하지만

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     LoginForm fLogin = new LoginForm(); 

     if (fLogin.ShowDialog() == DialogResult.OK) 
     { 
      Application.Run(new Form1(fLogin.UserName)); 

     } 
     else 
     { 
      Application.Exit(); 
     } 

     //Application.Run(new Form1()); 
    } 

희망이, 무엇을해야하는지에 대한 일반적인 아이디어를 제공합니다 이 작업을 더 잘 수행 할 수있는 방법이며, 실제로는 보안 프런트 엔드가 아니라는 점에 유의하십시오.

편집 :

희망이 도움이 오와 나는

Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName 

을 사용하지 말아 잊어 버리기 전에 그냥 저장 프로 시저에 던져 것입니다. 하지만 어쨌든이 방법은 인증 할 수있는 최선의 방법은 아니지만 적어도 작동하는 솔루션은 당신을 얻으려는 것입니다.

관련 문제