2

我最初是在 ASP.net 应用程序(非 MVC)上工作的,但现在我必须切换到 MVC,我不知道如何调整我的旧代码。作为参考,我正在使用您为应用程序获得的股票网站(需要快速而肮脏),并且我也在 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