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/p/1731461.html