在c#网页中自动登录无效

时间:2015-01-06 16:07:53

标签: c# active-directory

我的网页有一种奇怪的行为。 我已经进行了自动登录,获取了Active Directory用户名。 这完全适用于我的笔记本电脑(我将网络移动到我的笔记本电脑上的iis服务器)

问题是: 当另一位用户在另一台笔记本电脑(同一网络)上打开网页时,会出现一个自动弹出窗口,询问用户名和密码。

¿我怎么能避免这种情况?

希望你的帮助问候。

我的索引代码:

 public partial class index : System.Web.UI.Page
{
    string usuario_ldap, rut_empleado, username;
    UserPrincipal usuario_data;
    protected void Page_Load(object sender, EventArgs e)
    {


   username = HttpContext.Current.User.Identity.Name;
    string[] usuario = username.Split('\\');
        usuario_data = GetActiveDirectoryUser(usuario[1]);
        usuario_ldap = usuario_data.DisplayName;
        rut_empleado =  usuario_data.EmployeeId;

         if (isAdmin(rut_empleado) == true)
             {

                Session["administrador"] = true;              
             }
         else
              {
                Session["administrador"] = null;

              }

              Session["logon"] = usuario_ldap;

         if (Session["logon"] == null)
             {          
                 Response.Redirect(" login.aspx? name=" + Session["logon"]);

              }
         else
              {
              Response.Redirect("INGRESO.aspx");
             } 
    }



   public bool isAdmin(string rut)
     {

         string cs = ConfigurationManager.ConnectionStrings["conexionsql"].ConnectionString;
         SqlConnection con = new SqlConnection(cs);
                    try
                    {
                        con.Open();

                    }
                    catch (SqlException e)
                    {

                    }


  SqlCommand command = new SqlCommand("select * from IngT_Administrador where rut = @rut", con);
                    command.Parameters.AddWithValue("@rut", rut.Trim());

                   SqlDataReader reader = command.ExecuteReader();
                   if (reader.Read())
                    {
                        con.Close();
                        return true;
                    }
                    else
                    {
                        con.Close();
                        return false;
                    } 

                }

    private UserPrincipal GetActiveDirectoryUser(string userName)
    {
        using (var ctx = new PrincipalContext(ContextType.Domain))
        using (var user = new UserPrincipal(ctx) { SamAccountName = userName })
        using (var searcher = new PrincipalSearcher(user))
        {
            return searcher.FindOne() as UserPrincipal;
        }
    }

我的WebConfig是:

 <?xml version="1.0" encoding="UTF-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>


    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="*" />
      </assemblies>
    </compilation>

    <authorization>
      <deny users="?" />
    </authorization>


  </system.web>

  <connectionStrings> 
    <clear />
    <remove name="LocalSqlServer" />
   <add connectionString="data source=.\SQLEXPRESS;Integrated     Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" name="LocalSqlServer"/>

<add connectionString="Server=10.10.201.79;Database=DesaInterno;User ID=usuario_de_conexion;Password=a" name="conexionsql" />
</connectionStrings>

</configuration>

0 个答案:

没有答案