这几天在用MVC做一个项目,用到了HttpContext.User.IsInRole() 这个方法,但是每次当我用的时候,HttpContext.User.IsInRole(“Admin”) 返回的永远是false。 在网上查了很多资料,发现都没有解决,要解决的话,也要实现一系列的扩展方法。好,废话少说,正式进入主题:

权限判断
if (HttpContext.User.Identity == null || String.IsNullOrEmpty(HttpContext.User.Identity.Name))
 {
      return Redirect("~/Account/LogOn?returnUrl=/service");
 }
else if (HttpContext.User.IsInRole("Admin"))
  {
         return RedirectToAction("Index""AdminService");
 }
else
{
  …….
}

上面的代码中HttpContext.User.IsInRole(“Admin”) 返回的是false。我们要返回True怎么办?

Global.asax 中添加以下方法:

View Code
/// <summary>
/// Authen right for user
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
        {
            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        //Get current user identitied by forms
                        FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                        // get FormsAuthenticationTicket object
                        FormsAuthenticationTicket ticket = id.Ticket;
                        string userData = ticket.UserData;
                        string[] roles = userData.Split(',');
                        // set the new identity for current user.
                        HttpContext.Current.User = new GenericPrincipal(id, roles);
                    }
                }
            }
        }

添加好以后,进入你的登录页面,给当前用户授权。请看:

LogOn
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
   if (ModelState.IsValid)
   {
     if(ValidateUser(model.UserName, model.Password)))
     {
 UserInfo userInfo = GetuserInfo(model.UserName);
if (userInfo.Role =="Admin")                    {
    role = "Admin";
}
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
                        userInfo.Alias,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(30),
                        false,
                        role);
                    string encTicket = FormsAuthentication.Encrypt(authTicket);
                    this.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,encTicket));

                  //  FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index""Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("""The user name or password provided is incorrect.");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

 好了,直到这里,所有的问题,已经解决了。如果大家有其他的好的方法,可以分享, 欢迎留言指正 :)

posted @ 2012-01-10 10:54 tomin 阅读(1479) 评论(2) 编辑

They will chat your porject with you in english. and then wirter some algorithm,then write some test case.
 
 1. Give you a website which just implement one function: translating. such as: Chinese will be translated to English. there is a Textbox to input,a Button to click and a Label to return the result from translated.
 Question: please try to write more test cases to test this website.
 
 // The below algorithm is from Microsoft
2.  The function will return the count of str1 containing str2.
        such as: str1=abc, str2=ab, return 1
        str1=aaa, str2=aa, return 2 ....
  
        /// <summary>
        ///  The function will return the count of str1 containing str2.
        ///  such as: str1=abc, str2=ab, return 1
        ///  str1=aaa, str2=aa, return 2 ....
        /// </summary>
        /// <param name="str1"></param>
        /// <param name="str2"></param>
        /// <returns></returns>
        public int ContainNumber(string str1, string str2)
        {
            if (string.IsNullOrEmpty(str1))
                return 0;
            if (string.IsNullOrEmpty(str2))
                return 0;
            if (str1.Length < str2.Length)
                return 0;
            int number = 0;
            int i=0;
                while(str1.Length - i >= str2.Length)
                {
                    string temp = str1.Substring(i, str2.Length);
                    if (temp == str2)
                    {
                        number++;
                    }
                    i++;
                }
           
            return number;
        }

        /// <summary>
        /// the function will return the content between quotation in one string inputed.
        /// such as:   input str:  I am "from" china, I love "china". this sentence will return a list which take two eletements
        /// from, china.
        /// </summary>
        /// <param name="str"> </param>
        /// <returns></returns>
        public List<string> GetContentOfQuotation(string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            List<string> Qlist = new List<string>();
            int i=0;
            int start = 0;
            while (i < str.Length)
            {
                if (str[i] == '"')
                {
                    start = i;
                    i++;
                    while (i < str.Length)
                    {
                        if (str[i] == '"')
                        {
                            Qlist.Add(str.Substring(start+1, i - start-1));
                            break;
                        }
                        i++;
     }                  
                }
                i++;
            }
            return Qlist;
        }

       /*
        At last:  All the method, you should writte test case to test these method.      
       */

 

4. how to ensure night point on one circular?

-------------------------------------------------------------

5. reverse wards by space. And write test case

6. Given two parameters, string sentence and int length. Then you should cut the sentence by the specific length. But if the cut point located in a vocabulary, you move the cut point in the previous space in0 front of the vocabulary. And write test case

Example:

String : I love this game.

Length: 6

Result: I love

                this

                game.

 

String : I love basketball.

Length: 6

Result: I love

Give a message here: the cut point located in “basketball” which is a vocabulary

posted @ 2010-11-17 15:37 tomin 阅读(239) 评论(0) 编辑
       As last blog mentioned(中软面试题-最新) ,Some outsourcing company,like chinasoft, beyondsoft, vinceinfo, Hisoft,wicresoft, etc. all the process of interview is the same, if you pass the interview of these company, according to the condition of the project to decide whether you are recommended to Microsoft or not, if yes, you have to practice your oral English and some Algorithm. now go to the interview of Beyondsoft:

1.HR will have a interview face to face for your oral English and your ability of expressing your emotion for some thing. then Technical interviewer (Team Leader) will take some technology test.

2.数据库存储过程,ASP.NET 中怎么去调用存储过程。

    其实,在这里主要是考察了ADO.NET中几大特性的运用,例如:Connection,Command,DataReader,Dataset,

DataAdapter。

   

说明:
Connection:建立与特定数据源的连接。 所有 Connection 对象的基类均为 DbConnection 类。
Command:对数据源执行命令。 公开 Parameters,并可在 Transaction 范围内从 Connection 执行。 所有 Command 对象的基类均为 DbCommand 类。
DataReader:从数据源中读取只进且只读的数据流。 所有 DataReader 对象的基类均为 DbDataReader 类。
DataAdapter:使用数据源填充 DataSet 并解决更新。 所有 DataAdapter 对象的基类均为 DbDataAdapter 类。
注意:新手面试经常会遇到考这样的题:ADO.NET 的五大对象,就是 上面四种 + DataSet 要牢牢记住哦。后期开发也经常用到。

3. 简单介绍一下ASP.NET三层架构 可以参考我的博客: 浅析ASP.NET三层架构

4.在未排序的整形数组中,长度大于10,寻找任意一个数,是这个数属于它的最小集。
   对于这个问题,我不知道定义会不会有错,怎样去理解这个最小集,例如:有数组 a=(5,3,4,8,9,2,12,10),那在这个数组中,肯定有一个值 3,属于最小集(3,4)。那这样是不是很好理解呢.找出最小的,再找出第二小的。这样写出来它的时间复杂度就是 O(n),这时候,面试官问,有没有比O(n)更小的算法呢?读者可以考虑一下。
 5.XPath。考查一下xpath 的运用。其中文思创新喜欢考这玩意。在这里写一下小运用:

Xpath
public void ReadXmlNode(string filepath)
{
    XmlDocument xmldoc 
= new XmlDocument();
    xmldoc.Load(filepath);
    XmlNodeList nodelist 
= xmldoc.SelectNodes(@"/root/employees/employee"); // xpath

    
foreach (XmlNode node in nodelist)
    {
        Console.Write(node.Name 
+ " ");
    }
}

6.HeapSort。you can get more information from internet. below is source:

HeapSort
/// <summary>
/// 小根堆排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param>
/// <returns></returns>
private static void HeapSort(ref double[] dblArray)
{
    
for (int i = dblArray.Length - 1; i >= 0; i--)
    {
        
if (2 * i + 1 < dblArray.Length)
        {
            
int MinChildrenIndex = 2 * i + 1;
            
//比较左子树和右子树,记录最小值的Index
            if (2 * i + 2 < dblArray.Length)
            {
                
if (dblArray[2 * i + 1> dblArray[2 * i + 2])
                    MinChildrenIndex 
= 2 * i + 2;
            }
            
if (dblArray[i] > dblArray[MinChildrenIndex])
            {
                ExchageValue(
ref dblArray[i], ref dblArray[MinChildrenIndex]);
                NodeSort(
ref dblArray, MinChildrenIndex);
            }
        }
    }
}

/// <summary>
/// 节点排序
/// </summary>
/// <param name="dblArray"></param>
/// <param name="StartIndex"></param>
private static void NodeSort(ref double[] dblArray, int StartIndex)
{
    
while (2 * StartIndex + 1 < dblArray.Length)
    {
        
int MinChildrenIndex = 2 * StartIndex + 1;
        
if (2 * StartIndex + 2 < dblArray.Length)
        {
            
if (dblArray[2 * StartIndex + 1> dblArray[2 * StartIndex + 2])
            {
                MinChildrenIndex 
= 2 * StartIndex + 2;
            }
        }
        
if (dblArray[StartIndex] > dblArray[MinChildrenIndex])
        {
            ExchageValue(
ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);
            StartIndex 
= MinChildrenIndex;
        }
    }
}

/// <summary> 
/// 交换值
/// </summary>
/// <param name="A"></param>
/// <param name="B"></param>
private static void ExchageValue(ref double A, ref double B)
{
    
double Temp = A;
    A 
= B;
    B 
= Temp;
}    

7.智力题,在12个小球中有一个和其他不同(或轻或重),用一天枰。请问至少称几次可以称出来,怎么称。
   这样的题目,网上很多,主要考察的是一种逻辑思维能力。
8.设计模式---单键模式,工厂模式,观察者模式等等。
  在这里不做细致的分析,读者可以自己去网上找点资料找点。

 这次面试比较快,后面也被推到微软那边了,面试题,我将会在后面统一列出来。

 注: 本人只在这里做面试的技术和小小经验分享,不做任何的公司评价。给更多的程序员一个交流和发展的平台,更是给正在贫困线左右的找工作的大学生一个小分享。如有任何的问题,请留下你的足迹,后面,我同一修改。

posted @ 2010-07-29 10:29 tomin 阅读(4888) 评论(10) 编辑
摘要: 中软的面试比较经典,也比较严格,一般有四轮,类似于微软的面试。中软面过以后,根据项目组,会推到美国微软那边运用live meeting & con-call 再面一次。以下是我的面试题及个人的小分析,拿出来和大家share一下。希望更多的人能过这个坎。如有什么问题,可以一起交流。直接进入主题:1. English communication. (sale yourself, project...阅读全文
posted @ 2010-07-28 10:15 tomin 阅读(5700) 评论(64) 编辑

  最近,在和PM的聊天的中无意中接触到了Python 这门语言,其实,在大学的时候就已经知道了,但是没有用过。正好,在项目发布的时候有点时间来玩玩这门语言。Python作为一种功能强大且通用的编程语言而广受好评,它具有非常清晰的语法特点,适用于多种操作系统,目前在国际上非常流行,正在得到越来越多的应用。

下面就让我们一起来看看它的强大功能:
      Python
(派森),它是一个简单的、解释型的、交互式的、可移植的、面向对象的超高级语言。这就是对Python语言的最简单的描述。

       Python有一个交互式的开发环境,因为Python是解释运行,这大大节省了每次编译的时间。Python语法简单,且内置有几种高级数据结构,如字典、列表等,使得使用起来特别简单,程序员一个下午就可学会,一般人员一周内也可掌握。Python具有大部分面向对象语言的特征,可完全进行面向对象编程。它可以在MS-DOSWindowsWindows NTLinuxSolorisAmigaBeOSOS/2VMSQNX等多种OS上运行。

  编程语言

      Python语言可以用来作为批处理语言,写一些简单工具,处理些数据,作为其他软件的接口调试等。Python语言可以用来作为函数语言,进行人工智能程序的开发,具有Lisp语言的大部分功能。Python语言可以用来作为过程语言,进行我们常见的应用程序开发,可以和VB等语言一样应用。Python语言可以用来作为面向对象语言,具有大部分面向对象语言的特征,常作为大型应用软件的原型开发,再用C++改写,有些直接用Python来开发。

  数据库

      Python在数据库方面也很优秀,可以和多种数据库进行连接,进行数据处理,从商业型的数据库到开放源码的数据库都提供支持。例如:Oracle,Ms SQL Server等等。有多种接口可以与数据库进行连接,至少包括ODBC。有许多公司采用着PythonMySql的架构。因此,掌握了Python使你可以充分利用面向对象的特点,在数据库处理方面如虎添翼。

 Windows编程

      Python不仅可以在Unix类型的操作系统上应用,同样可以在Windows系统里有很好的表现。通过添加PythonWin模块,就可以通过COM形式调用和建立各种资源,包括调用注册表、ActiveX控件以及各种COM等工作,最常见的例子就是通过程序对Office文档进行处理,自动生成文档和图表。
      通过Python,还可以利用py2exe模块生成exe应用程序。还有许多其他的日常维护和管理工作也可以交给Python来做,从而减少维护的工作量。利用Python,你还可以开发出象VB,VC,Delphi那样的GUI程序,但却可以在多个平台上执行。这在许多方面并不逊色于Java

多媒体
 
      利用PILPiddleReportLab等模块,你可以处理图象、声音、视频、动画等,从而为你的程序添加亮丽的光彩。动态图表的生成、统计分析图表都可以通过Python来完成。另外,还有OpenGL。利用PyOpenGl模块,你可以非常迅速的编写出三维场景。

 科学计算
      Python可以广泛的在科学计算领域发挥独特的角色。有许多模块可以帮助你在计算巨型数组、矢量分析、神经网络等方面高效率完成工作。尤其是在教育科研方面,可以发挥出独特的优势。

网络编程
      Python可以非常方便的完成网络编程的工作,提供了众多的解决方案和模块,可以非常方便的定制出自己的服务器软件,无论是c/s,还是b/s模式,都有很好的解决方法。

 

Python写的较大的系统有:

 

 Zope:一个应用程序服务器,具有内容管理、团队开发、XML、面向对象、SOAP接口等一系先进特性,且开放源码。参见:www.zope.com

 

 Gadfly:一个用Python写的面向对象关系型数据库,具有小巧、快速、可移植性好,具有大部分SQL语言特性,且开放源码。参见:http://www.chordate.com/gadfly.html

 

 还有一些比较有名的公司也在用Python进行原型的开发,如MicrosoftIBM等,还有:

 

红帽(Red Hat )曾用PythonTk一起成功开发配置和管理操作系统的可视界面。整个系统可以全面控制Linux操作系统,并根据用户选择对配置文件作自动更新。

 

Infoseek在其公用搜索引擎使用了Python。该公司还用Python对其软件进行定制,使最终用户能对该网站内容进行方便下载。 

posted @ 2010-06-24 15:42 tomin 阅读(343) 评论(0) 编辑
摘要: 今天非常兴奋,因为昨天微软已经发布了 Windows Intune Beta 版。随着网易及其他的媒体的大力宣传,这款产品将越来越为人知。这也是我来公司后亲身经历的第二个产品的发布。这也是我亲手测过的产品。很开心,在这里特地把一篇 In a blog post on the Windows team blog 拿出来翻译一下,让园区更多的人来了解这个产品,后面,我也会分享更多的关于这个产品的信息,...阅读全文
posted @ 2010-04-20 15:13 tomin 阅读(1486) 评论(4) 编辑
摘要: 在VBA编程中,也有debug的功能使用,但与C# 及C++ 有点不同。特别在watch某个对象的时候,必须得选中,然后按shift +F9可以进行 quick watch. 请注意以下几点:· 插入断点,把鼠标放到某一行的代码前面,左击鼠标就可以插入断点,取消断点只要在断点上左击鼠标就ok· 单步调式――――  F8。可以用鼠标...阅读全文
posted @ 2010-03-30 17:53 tomin 阅读(817) 评论(0) 编辑
摘要: 由于工作一直都比较忙,没有来的及 及时发博客,今天来看看,发现,这个系列还没弄完。接着来~常用空间有Button, checkbox, combo box, list box, textbox, option button, 如果了解C# 控件的使用方法的话,那么这个是一样的用。以下代码主要是从某个sheet中获取某一列不重复的数据,填充到combo box 中 Sub InitComboBox ...阅读全文
posted @ 2010-03-30 17:29 tomin 阅读(540) 评论(0) 编辑
摘要: sheet中使用的所有使用的范围:Sub ClearCell() Dim sheet As WorksheetSet sheet = Sheets ("Temp")Dim used As RangeSet used = sheet.UsedRangeused.clearEnd Sub针对Rang对象还有很多的方法,例如,上面的代码可以这样去改:Sub clear(row As Integer)Di...阅读全文
posted @ 2010-01-15 09:20 tomin 阅读(289) 评论(1) 编辑
摘要: 很多人肯定知道宏,可能也用过宏,那什么是宏?宏是VBA语言编出的一段程序,是一系列命令和函数,存储于 Visual Basic 模块中,并且在需要执行该项任务时可随时运行。怎么开始学习VBA?这是不少吧友常问的问题之一。个人认为学习什么东西,最好的开始是兴趣,本贴的创建的原因也在于此。后面的内容可能大家在工作与生活中都没有什么实际的用处,但我希望后面的内容能引起大家对VBA编程的兴趣。而开始学习V...阅读全文
posted @ 2010-01-13 10:43 tomin 阅读(842) 评论(0) 编辑