Cod*_*ker 9 c# forms-authentication
我正在使用表单身份验证并具有基本登录页面和默认页面.
当我在登录页面上,并调用SignOn时,这非常有用.但是,当我仍在登录页面时,Membership.GetUser()返回null.当我重定向到我的默认页面时,Membership.GetUser()返回我的用户信息.
有没有办法让我的方法在登录页面上返回我的用户.我已经阅读了谷歌的所有其他类似的问题,它只会在你重定向后才能工作.
如果您需要更多信息,请与我们联系.
这是我用来验证信息设置的简单代码片段:
bool authenticated = User.Identity.IsAuthenticated;
string username = User.Identity.Name;
MembershipUser user = Membership.GetUser();
Run Code Online (Sandbox Code Playgroud)
我将此代码放在登录页面和后面代码中的默认页面上,并且只有默认页面具有值,并显示在身份验证过程执行后对其进行身份验证.
小智 13
还有其他要尝试的是以下代码:
MembershipUser user = Membership.GetUser(username);
GenericIdentity identity = new GenericIdentity(user.UserName);
RolePrincipal principal = new RolePrincipal(identity);
System.Threading.Thread.CurrentPrincipal = principal;
HttpContext.Current.User = principal;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13037 次 |
| 最近记录: |