.NET程序员面试问题,虚心求教高手!在这段程序中(header1.cs),我们已经完成了以下几个任务:
1)查看我们是否联接上了服务器,并显示相应的连接。
2)查看和制定在头文件(header)中数据显示的格式。
3)在头文件(header)中建立“home”连接。-- 问题:请问你如何修改这段程序来实现下面的功能:
        如果我们发现我们联接上了服务器,我们想显示一段文字“你连接上了服务器”;
        如果我们发现我们没有联接上服务器,我们想显示一段文字“你没有连接上服务器”;using System;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;
using System.Security;
using System.Security.Principal;
using System.Security.Cryptography;
using SecureSGD;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Text;public class HeaderClass : UserControl
{public Label TodaysDate;
public Image Head1;
public Image Head2; 
public HyperLink DispLoginLink;
public HyperLink ControlPanel;
public HyperLink DispLogOut;
public HyperLink HomeLink;
public Label Divider; public void Page_Load()
{
       DisplayItems();
DateTime GetTDate = DateTime.Now;
TodaysDate.Text = GetTDate.ToString("D", null);
Random rand = new Random(); 
int randValue = rand.Next(1,10); 
string ImageVal = randValue.ToString();
Head1.ImageUrl = "/master/templates/demo/images/header/random/usac_headimg" + ImageVal + "a.jpg";
Head2.ImageUrl = "/master/templates/demo/images/header/random/usac_headimg" + ImageVal + "b.jpg";

string PartnerInfo;
       if(Request.QueryString["Partner"] == null || Request.QueryString["Partner"].Length == 0)
       {
           PartnerInfo = "";
        }
       else
       {
       PartnerInfo = "?partner=" + Request.QueryString["Partner"];
       } if(CheckRole("4"))
      {
           HomeLink.NavigateUrl = "/master/DSE-MASTER/index_private.aspx";
       }
       else
       {
           HomeLink.NavigateUrl = "/master/DSE-MASTER/index.aspx" + PartnerInfo;
       }

              DataBind();
   }       public bool CheckRole(string Role)
{
           string UserIP = Request.ServerVariables["REMOTE_ADDR"].ToString();
    string AccessId = Request.QueryString["AccessId"];
    if(UserIP == "usa-svr2app-sql" && AccessId == "csweb")
    {
return true;
    }
    else
    {

    }

    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Context.Request.Cookies[cookieName];

    if(null == authCookie)
    {
//没有cookie被发现
return false;
    }

    FormsAuthenticationTicket authTicket = null;
    try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
    catch(Exception ex)
      {
              return false;
}

    if(null == authTicket)
   {
               return false;
    }
    else
   {

string[] roles = authTicket.UserData.Split(new char[]{'|'});
FormsIdentity id = new FormsIdentity(authTicket);

GenericPrincipal principal = new GenericPrincipal(id, roles);

Context.User = principal;

IPrincipal p = HttpContext.Current.User;
return p.IsInRole(Role);

     }
return false;
}

private void DisplayItems()
{

ControlPanel.Visible = CheckRole("4");
if(CheckRole("4") || CheckRole("7"))
{
   DispLogOut.Visible = true;
}
else
{
   DispLogOut.Visible = false;
}
Divider.Visible = CheckRole("4");

if(DispLogOut.Visible == true)
{
   DispLoginLink.Visible = false;
}
}
}

解决方案 »

  1.   

    在哪里加呢,是在DisplayItems()前面吗?
      

  2.   

    是不是只要判断一下checkrole()这个函数?
      

  3.   

    if(CheckRole("4"))
          {
               HomeLink.NavigateUrl = "/master/DSE-MASTER/index_private.aspx";
                         Response.Write("你连上服务器啦");
           }
           else
           {
               HomeLink.NavigateUrl = "/master/DSE-MASTER/index.aspx" + PartnerInfo;
                         Response.Write("你没有连上服务器啦");
           }
      

  4.   

    小弟是新手,可不可以在DisplayItems()里修改呢,请高手指出错误 if(CheckRole("4") || CheckRole("7"))
    {
       DispLogOut.Visible = true;
                            Response.Write("你连上服务器啦");
    }
    else
    {
       DispLogOut.Visible = false;
                            Response.Write("你没有连上服务器");
    }
      

  5.   

    pool66():
            我认为可以,不过我也是新手!
      

  6.   

    如果你没连上服务器,换句话说,如果服务器不存在,你这代码判断还有意义吗?肯定是iis都解析出错了
      

  7.   

    能不能象ping那样的来验证是否连接到了服务器呢~?
      

  8.   

    抱歉,抱歉,我的英语太差,可能翻译错误,应该是登陆吧,英文原文如下:
    In this example(header1.cs), we do several things :
         1) Check to see if we are logged in and display links appropriately
         2) Check and format date display in header
         3) Set the "home" link for the header
       - If we find that we are logged in to the system, we want to display 
    the text "You are logged in"
       - If we find that we are not logged in, we want to display the text 
    "You are not logged in"
       - Can you make a recommendation on how we would alter the current 
    code to accomplish that?
      

  9.   

    这不是填空题吗,login是登陆,就是 检查 cookies
    if(null == authCookie)
        {
    //没有cookie被发现
    return false;
        }
    人家考试的不都告诉你在这里填了吗?
      

  10.   

    很多地方可以判断的
    他这段代码主要功能不算复杂了,CheckRole(string Role)是用来检查是否登陆的函数,除了一个特定的ip和accessid,别的都是通过form 认证方式来认证身份的。DisplayItems()只是用来控制显示菜单的。
    按照他的要求个人感觉写在DisplayItems()比较的好,毕竟这个函数只要是控制界面部分的,而要求的就是界面部分的信息显示了:)个人看法,不一定正确了!