WebService来获取Context.User.Identity.Name为空的问题?
2010-05-10 01:10
543 查看
在程序的设计中遇到一些问题,晚上查资料很久都没有解决,希望大家能够帮我看一下。
我首先为站点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站点中使用JQuery的Ajax调用webservice进行一些数据的互操作,咨询您之后决定采用context.user.identity 来传递是否登录和登录用户的Userid的信息,于是我就构建了PageIdentity.cs和PagePrincipal.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
我首先为站点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站点中使用JQuery的Ajax调用webservice进行一些数据的互操作,咨询您之后决定采用context.user.identity 来传递是否登录和登录用户的Userid的信息,于是我就构建了PageIdentity.cs和PagePrincipal.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
相关文章推荐
- 集成平台自动登入+C# Login控件,造成的HttpContext.Current.User.Identity.Name 为空问题解决方案
- MVC002之获取当前用户失败(Context.User.Identity.Name)
- HttpContext.Current.User.Identity.Name 有时获取值不成功
- MVC: [NonAction] 里面的获取不到用户登录信息 User.Identity.Name
- 解决User.Identity.IsAuthenticated==false 或User.Identity.Name==string.empty的问题
- 添加 <identity impersonate="true" userName="username" password="password"/>,解决问题!
- 解决CXF获取客户端IP(WebServiceContext为null问题)
- 解决User.Identity.IsAuthenticated==false 或User.Identity.Name==string.empty的问题
- Page.User.Identity.Name获取不到结果
- Page.User.Identity.Name获取不到值
- asp.net Context.User.Identity.Name说明
- 添加 <identity impersonate="true" userName="username" password="password"/>,解决问题!
- 解决员工更换工号后通过SPContext.Current.Web.CurrentUser.LoginName查找当前登录用户还是原来工号的问题
- 解决User.Identity.IsAuthenticated==false 或User.Identity.Name==string.empty的问题
- CXF获取客户端IP(解决WebServiceContext为null问题)
- CXF获取客户端IP(解决WebServiceContext为null问题)
- asp.net Context.User.Identity.Name说明
- Context.User.Identity.Name说明
- 一例应用Microsoft.AspNet.Identity初始化角色权限导致的UserManager.GetRoles(user.Id)始终获取不到账号角色问题的解决
- User.Identity.Name获取为空解决方法