HttpContext.User.Idenity 为空

发布于 2024-08-19 17:02:47 字数 679 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎樣才叫好 2024-08-26 17:02:47

您的操作是为当前上下文设置用户 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

农村范ル 2024-08-26 17:02:47

我认为问题在于您只是将用户设置为经过身份验证,因此 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.

怎会甘心 2024-08-26 17:02:47

我也很挣扎。

我试图使用标准 ASP.Net 成员资格和角色提供程序在 WCF 服务内执行身份验证和授权。

我想传递凭据和“请求的应用程序”来确定用户是否通过该应用程序的“身份验证”。 (不是 ASP.Net 应用程序,而是我自己的数据库中的应用程序)。

为此,我想要访问这些角色,但不想“重定向”或再次调用我的 WCF 服务。

这是一些适合我的代码:

首先,我确定用户是否有效,如下所示:

if (Membership.ValidateUser(CompanyCn, CompanyPwd))
{
    sbLogText.AppendFormat("\r\n\r\n\tValid User UID/PWD: '{0}'/'{1}'", CompanyCn, CompanyPwd);
    FormsAuthentication.SetAuthCookie(CompanyCn, false);
}

然后以下代码可以很好地获取角色列表:

List<string> roleList = new List<string>(Roles.GetRolesForUser(CompanyCn));
sbLogText.AppendFormat("\r\n\r\n\tUser ('{0}'): Roles ({1}):", CompanyCn, roleList.Count);
foreach (string s in roleList)
    sbLogText.AppendFormat("\r\n\t\tRole: {0}", s);

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:

if (Membership.ValidateUser(CompanyCn, CompanyPwd))
{
    sbLogText.AppendFormat("\r\n\r\n\tValid User UID/PWD: '{0}'/'{1}'", CompanyCn, CompanyPwd);
    FormsAuthentication.SetAuthCookie(CompanyCn, false);
}

Then the following code workes nicely for getting the list of roles:

List<string> roleList = new List<string>(Roles.GetRolesForUser(CompanyCn));
sbLogText.AppendFormat("\r\n\r\n\tUser ('{0}'): Roles ({1}):", CompanyCn, roleList.Count);
foreach (string s in roleList)
    sbLogText.AppendFormat("\r\n\t\tRole: {0}", s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文