读书人

关于.net的身份认证有关问题

发布时间: 2013-07-16 22:38:05 作者: rapoo

求助关于.net的身份认证问题
本帖最后由 ss27588709 于 2013-07-13 19:43:47 编辑 当用户登录成功后,使用:

FormsAuthentication.SetAuthCookie(userName, false)颁发身份认证。

userName不是空的。

即便在同一个页面,不跳转,继续使用HttpContext.Current.User.Identity.Name时,发现为空。或者HttpContext.Current.User.Identity.IsAuthenticated发现是false。

通过IIS日志查询,发现.ASPXAUTH却是有值的。

在Web.config我的配置是

xxxxx是我网站的域名。

<authentication mode="Forms">
<forms loginUrl="Account/Login.aspx" domain="xxxxx" protection="All" timeout="60" name=".ASPXAUTH" path="/" slidingExpiration="true" defaultUrl="~/" cookieless="UseDeviceProfile" enableCrossAppRedirects="false" />
</authentication>

有朋友遇见过这样的问题吗?求解答。

并不是所有的用户都会出现这样的情况,大概有百分之1的样子。浏览器一般为IE8 .NET SetAuthCookie Membership .Net登录
[解决办法]
需要给用户授予登陆的票据FormsAuthenticationTicket参考
http://blog.csdn.net/byondocean/article/details/7164117
[解决办法]
你需要在Global.asax中增加一些设置。例如:

        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)
{
FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;


FormsAuthenticationTicket ticket = id.Ticket;

// 取存储在票据中的用户数据,在这里其实就是用户的角色
string userData = ticket.UserData;
string[] roles = userData.Split(',');
HttpContext.Current.User = new GenericPrincipal(id, roles);
}
}
}
}

读书人网 >asp.net

热点推荐