HttpContext.User.Idenity 为空
我正在使用 asp.net 并尝试为具有表单身份验证的用户分配角色,如下所示:
public ActionResult AdminLogin(string password, string username)
{
User _user = _us.GetUsers(username, password).FirstOrDefault();
if (_user != null)
{
string _username = _user.Username;
FormsAuthentication.SetAuthCookie(_username, false);
string[] _roles = _us.GetUserRoles(_username);
HttpContext.User = new GenericPrincipal(HttpContext.User.Identity, _roles);
return RedirectToAction("Index", "Admin");
当我调试 HttpContext.User.Identity 时,它始终为 null,但 _username 和 _roles 包含正确的数据。如何解决这个问题?
/米
I'm using asp.net and trying to assign roles for a user with forms authentication like this:
public ActionResult AdminLogin(string password, string username)
{
User _user = _us.GetUsers(username, password).FirstOrDefault();
if (_user != null)
{
string _username = _user.Username;
FormsAuthentication.SetAuthCookie(_username, false);
string[] _roles = _us.GetUserRoles(_username);
HttpContext.User = new GenericPrincipal(HttpContext.User.Identity, _roles);
return RedirectToAction("Index", "Admin");
When I debug HttpContext.User.Identity always is null, but _username and _roles contains the proper data. Howto fix this?
/M
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的操作是为当前上下文设置用户 IPrincipal。一旦您重定向到其他操作(以及所有后续请求),就会创建一个带有空用户 IPrincipal 的新 HttpContext。
您可以做的是将信息保留在身份验证 cookie 中,然后在 Global.asax 文件的 Application_AuthenticateRequest 方法中提取该数据,并在其中设置 HttpContext 的 User 属性。
此答案包含更多详细信息和示例代码
Your action is setting the User IPrincipal for the current context. As soon as you redirect to your other action (and all subsequent requests) a new HttpContext is created with a null User IPrincipal.
What you could do is persist the information in the authentication cookie and then extract that data in the Application_AuthenticateRequest method in your Global.asax file and set the User property of the HttpContext there.
This answer contains more details and example code
我认为问题在于您只是将用户设置为经过身份验证,因此 HttpContext 尚未更新,因为尚未在请求的用户端设置身份验证 cookie。
I believe the issue is that you are just setting the user as authenticated, and therefore, the HttpContext is not updated yet since the auth cookie has not yet been set on the users side of the request.
我也很挣扎。
我试图使用标准 ASP.Net 成员资格和角色提供程序在 WCF 服务内执行身份验证和授权。
我想传递凭据和“请求的应用程序”来确定用户是否通过该应用程序的“身份验证”。 (不是 ASP.Net 应用程序,而是我自己的数据库中的应用程序)。
为此,我想要访问这些角色,但不想“重定向”或再次调用我的 WCF 服务。
这是一些适合我的代码:
首先,我确定用户是否有效,如下所示:
然后以下代码可以很好地获取角色列表:
I was struggling too.
I was trying to carryout my authentication and authorization inside a WCF service using standard ASP.Net Membership and Role providers.
I wanted to pass in credentials and a 'requested app' to determine if the user 'authenticated' for that app. (not the ASP.Net APP, but an app in my own database).
To do this, I wanted access to the roles, but didn't want to 'redirect' or have a second call to my WCF service.
Here is some code that works for me:
First I determine if the user is valid as follows:
Then the following code workes nicely for getting the list of roles: