2

私はもともとこれを ASP.net アプリケーション (MVC 以外) で動作させていましたが、MVC に切り替える必要があるため、古いコードをどのように適応させるかわかりません。参考までに、私はアプリケーション用に取得したストック Web サイトを使用しており (迅速で汚れている必要があります)、Zurb の Foundation フレームワークも使用しています。これも C# ベースです。

これがうまくいった古い方法です:

ログイン.ASPX

<form id="Login" method="post"  runat="server">
          <fieldset>
              <legend>Please login</legend>
                    <asp:Label ID="errorLabel" Runat="server" ForeColor=#ff3300></asp:Label><br>

              <div class="row">
                  <div class="large-12 columns">
                      <label>Domain:</label>
                      <asp:TextBox ID="txtDomain" Runat="server" placeholder="Human Check: Please type WORKGROUP"></asp:TextBox>
                  </div>
              </div>
              <div class="row">
                  <div class="large-12 columns">
                      <label>Username:</label>
                       <asp:TextBox ID=txtUsername Runat="server" ></asp:TextBox>
                  </div>
              </div>
              <div class="row">
                  <div class="large-12 columns">
                      <label>Password:</label>
                        <asp:TextBox ID="txtPassword" Runat="server" TextMode=Password></asp:TextBox><br>
                  </div>
              </div>
              <div class="row">
                  <div class="large-6 columns">
<%--                      <a href="#" class="button" id="btnLogin"  runat="server"  önclick="Login_Click">Submit</a>--%>
                      <asp:Button ID="Button1" Runat="server" Text="Login" OnClick="Login_Click" CssClass="button"></asp:Button>
                  </div>
                  <div class="large-6 columns">
                    <br />
                      <asp:CheckBox ID=chkPersist Runat="server" /> Remember Me                  
                  </div>

              </div>
          </fieldset>
      </form>

以下のスクリプト(同じページ)が機能しました。

<script  runat="server">
void Login_Click(object sender, EventArgs e)
{
  string adPath = "LDAP://DC03/DC=Meowmeow,dc=com"; //Path to your LDAP directory server
  Legend_Forms_Manager.LdapAuthentication adAuth = new Legend_Forms_Manager.LdapAuthentication(adPath);
  try
  {
      if (true == adAuth.IsAuthenticated(txtDomain.Text, txtUsername.Text, txtPassword.Text))
      {
      string groups = adAuth.GetGroups();

      //Create the ticket, and add the groups.
      bool isCookiePersistent = chkPersist.Checked;
      FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, 
                txtUsername.Text,DateTime.Now, DateTime.Now.AddMinutes(60), isCookiePersistent, groups);

      //Encrypt the ticket.
      string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

      //Create a cookie, and then add the encrypted ticket to the cookie as data.
      HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

      if(true == isCookiePersistent)
      authCookie.Expires = authTicket.Expiration;

      //Add the cookie to the outgoing cookies collection.
      Response.Cookies.Add(authCookie);

      //You can redirect now.
      Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUsername.Text, false));
    }
    else
    {
      errorLabel.Text = "Authentication did not succeed. Check user name and password.";
    }
  }
  catch(Exception ex)
  {
    errorLabel.Text = "Error authenticating. " + ex.Message;
  }
}
</script>

LdapAuthentication.cs

using System;
using System.Text;
using System.Collections;
using System.DirectoryServices;

namespace Legend_Forms_Manager
{
    public class LdapAuthentication
    {
        private string _path;
        private string _filterAttribute;

        public LdapAuthentication(string path)
        {
            _path = path;
        }

        public bool IsAuthenticated(string domain, string username, string pwd)
        {
            string domainAndUsername = domain + @"\" + username;
            DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd, AuthenticationTypes.SecureSocketsLayer);

            try
            {
                //Bind to the native AdsObject to force authentication.
                object obj = entry.NativeObject;

                DirectorySearcher search = new DirectorySearcher(entry);

                search.Filter = "(SAMAccountName=" + username + ")";
                search.PropertiesToLoad.Add("cn");
                SearchResult result = search.FindOne();

                if (null == result)
                {
                    return false;
                }

                //Update the new path to the user in the directory.
                _path = result.Path;
                _filterAttribute = (string)result.Properties["cn"][0];
            }
            catch (Exception ex)
            {
                throw new Exception("Error authenticating user. " + ex.Message);
            }

            return true;
        }

        public string GetGroups()
        {
            DirectorySearcher search = new DirectorySearcher(_path);
            search.Filter = "(cn=" + _filterAttribute + ")";
            search.PropertiesToLoad.Add("memberOf");
            StringBuilder groupNames = new StringBuilder();

            try
            {
                SearchResult result = search.FindOne();
                int propertyCount = result.Properties["memberOf"].Count;
                string dn;
                int equalsIndex, commaIndex;

                for (int propertyCounter = 0; propertyCounter < propertyCount; propertyCounter++)
                {
                    dn = (string)result.Properties["memberOf"][propertyCounter];
                    equalsIndex = dn.IndexOf("=", 1);
                    commaIndex = dn.IndexOf(",", 1);
                    if (-1 == equalsIndex)
                    {
                        return null;
                    }
                    groupNames.Append(dn.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1));
                    groupNames.Append("|");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error obtaining group names. " + ex.Message);
            }
            return groupNames.ToString();
        }
    }
}

以下の参考文献を含めました。

~ System.DirectoryServices

2008 年にさかのぼらないチュートリアルで一貫性のあるイオタがある場所を見つけるのは非常に困難です。

もしよろしければ私を助けてください... 私はここにすべてを持っていますが、今は翻訳する必要があると思います。

.aspx と .cs を古いものから新しいものに追加し、ADConnectionString を web.config に追加し、.cs と .aspx にトークンを追加して、クロスサイト スクリプティングを防止しました (参照に従って強制されました)。ページにアクセスして情報を入力できますが、[送信] をクリックするとページが空白になり、何もしません。まだ助けが必要です。

4

0 に答える 0