最难的.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.   

    连接服务器的意思就是log in. 有高手知道答案吗,等待中
    这是一个外企的面试题,英文原题如下,我可能翻译的太不好了:
    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?
      

  2.   

    柯南的意思是不用cookie, 而是用session吗? 为什么用cookie就不行呢。
      

  3.   

    这个好像不难,要不要用什么Cookie 、Session
      

  4.   

    1.是在public bool CheckRole(string Role)里连服务器的吧.
    3.<asp:HyperLink id="HyperLink1" runat="server"><a href ="home.aspx">HyperLink</a></asp:HyperLink>是不是这个意思
    不觉得这份题有什么好.可能我没理解吧
      

  5.   

    if (CheckRole("4") || CheckRole("7"))
    {
    DispLogOut.Visible = true;
    }
    else
    {
    DispLogOut.Visible = false;
    }
    这段是验证有没有连接到服务器的判断语句吧,在这里添加要求做的事情就可以了。我的理解是这样的,不知道对不对。
      

  6.   

    .NET QQ讨论群 3758188欢迎大家加入 注明你来自CSDN 谢谢!!!
      

  7.   

    小弟写了一个,很简单,因为小弟是菜鸟!
    RainControl.ascx
    ---------------------------------------------------------------------------
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="RainControl.ascx.cs" Inherits="Sunweidong.Other.RainControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <asp:Label id="lblMessage" runat="server">Label</asp:Label><br>
    <asp:Label id="lblDate" runat="server">Label</asp:Label>
    <br>
    <asp:HyperLink id="hlnkHome" runat="server">HyperLink</asp:HyperLink>----------------------------------------------------------------------------
    RainControl.ascx.cs
    -----------------------------------------------------------------------------
    namespace Sunweidong.Other
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls; /// <summary>
    /// Summary description for RainControl.
    /// </summary>
    public class RainControl : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Label lblMessage;
    protected System.Web.UI.WebControls.HyperLink hlnkHome;
    protected System.Web.UI.WebControls.Label lblDate; private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    DealLogin();
    CheckDate();
    ShowLink();
    }
    //
    private void DealLogin()
    {
    if(Request.Cookies["UserName"] != null && Request.Cookies["UserName"].Value != "")
    {
    this.lblMessage.Text = "You are logged in";
    }
    else
    {
    this.lblMessage.Text = "You are not logged in";
    }
    }
    //
    private void CheckDate()
    {
    this.lblDate.Text = DateTime.Now.ToShortDateString();
    }
    //
    private void ShowLink()
    {
    this.hlnkHome.NavigateUrl = "Default.aspx";
    this.hlnkHome.Text = "Home";
    }
    //
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }--------------------------------------------------------------------------------