WebService来获取Context.User.Identity.Name为空的问题?

在程序的设计中遇到一些问题,晚上查资料很久都没有解决,希望大家能够帮我看一下。

 

我首先为站点A的所有页面写了一个基类BasePage.cs

 

public    class  PageBase:System.Web.UI.Page
{
    
protected   override   void  OnInit(EventArgs e)
        {
             
// 我在这里判断了用户是否登录
            Bool islogin;
            If(islogin)
            {
            }
            Else
            {
               Response.redirect(AAA);
             }
        }
}

 

 

 

 

因为现在想在A站点中使用JQueryAjax调用webservice进行一些数据的互操作,咨询您之后决定采用context.user.identity 来传递是否登录和登录用户的Userid的信息,于是我就构建了PageIdentity.csPagePrincipal.cs,由于前面已经判断了用户是否登录,所以我选择在这里直接默认为登录的状态:

 

PageIdentity.cs

class  PageIdentity:System.Security.Principal.IIdentity
    {
        
private   string  userid  =   " -1 " ;
        
private   bool  isauthenticated  =   false ;
        
public  PageIdentity( string  m_userid,  string  m_userpass)
        {
            userid 
=  m_userid;
            isauthenticated 
=   true ;
        }
        
#region  IIdentity 成员
        
public   string  AuthenticationType
        {
            
get  {  return   null ; }
        }
        
public   bool  IsAuthenticated
        {
            
get  {  return  isauthenticated; }
        }
        
public   string  Name
        {
            
get  {  return  userid; }
        }
        
#endregion
}

 

 

 

PagePrincipal.cs

class  PagePrincipal:System.Security.Principal.IPrincipal
    {
        
private  System.Security.Principal.IIdentity identity;
        
private   bool  isinrole  =   false ;
        
public  PagePrincipal( string  userid,  string  userpass)
        {
            identity 
=   new  PageIdentity(userid, userpass);
            
if  (identity.IsAuthenticated)
            {
                isinrole 
=   true ;
            }
        }
        
#region  IPrincipal 成员
        
public  System.Security.Principal.IIdentity Identity
        {
            
get  {  return  identity; }
        }
        
public   bool  IsInRole( string  role)
        {
            
return  isinrole;
        }
        
#endregion
    }

 

 

 

然后修改了BasePage.cs来设置A站点页面的Context.User.

public    class  PageBase:System.Web.UI.Page
{
protected   override   void  OnInit(EventArgs e)
        {
             
// 我在这里判断了用户是否登录
            Bool islogin;
            If(islogin)
            {
                    
PagePrincipal pageuser  =   new  PagePrincipal(userid,  " null " );
                    Context.User  =  pageuser;
                    System.Web.Security.FormsAuthentication.SetAuthCookie(UserID,  true ,System.Web.Security.FormsAuthentication.FormsCookiePath);
}
Else
{
    Response.redirect(AAA);
}
        }
}

 

 

 

 

在站点A中我使用this.Title = HttpContext.Current.User.Identity.Name;可以取到Name的值,

但是在WebService中,HttpContext.Current.User.Identity.Name的值却是空值,而且Context.User.Identity.IsAuthenticated也是false值。

以下是我的WebService的代码:

[System.Web.Script.Services.ScriptService]
    
public   class  Service : System.Web.Services.WebService
    {
 
        [WebMethod]
        
public   string  GetLoginUserID()
        {
            
string  uid  =   " -1 " ;
            
if  (Context.User.Identity != null )
            {
                
if  ( ! string .IsNullOrEmpty(Context.User.Identity.Name))
                {
                    uid 
=  Context.User.Identity.Name;
                }
              
            }
            
return  uid;
        }
}

但是这里一直取不到 Context.User.Identity.Name的值,并且通过调试发现Context.User.Identity.IsAuthenticated也是false的值,不知道为什么。

 

该问题一直没有解决,希望大家能够帮我看一下是哪里存在问题,不胜感激。

 

附件是相应的程序代码:

/Files/yangrui/project.rar

转载于:https://www.cnblogs.com/yangrui/archive/2010/05/10/1731461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值