【问题标题】:Unable to get the Current.User.Identity using formsauthentication无法使用 formsauthentication 获取 Current.User.Identity
【发布时间】:2011-05-11 21:35:29
【问题描述】:

我是 asp.net mvc 2 的新手。我正在通过做一个测试项目来学习 mvc。 我正在使用表单身份验证登录我的网站。在登录期间,我可以登录网站,但我没有获得用户身份。

我已将http://www.dotnetfunda.com/articles/article141.aspx 作为我的参考网站

在我的 webconfig 文件中。

<authentication mode="Forms">
      <forms loginUrl="~/Home/login" defaultUrl="~/Home/login" cookieless="UseCookies" slidingExpiration="true" timeout="20" />
</authentication>

在登录控制器中

public ActionResult Login(LogOnModel logon)
{
    if (ModelState.IsValid)
    {
        if (FormsService.SignIn(logon.UserName, logon.Password) == true)
        {
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, // version 
                                                    FormsService.Username, // user name
                                                    DateTime.Now, // create time
                                                    DateTime.Now.AddSeconds(30), // expire time
                                                    false, // persistent
                                                    FormsService.Role,
                                                    FormsAuthentication.FormsCookiePath); // user data, such as roles

            string hashCookies =   FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
            Response.Cookies.Add(cookie);
            if (FormsService.Role == "Brand")
            {
                return RedirectToAction("Index", "Creative");
            }
            else if (FormsService.Role == "Creative")
            {
                return RedirectToAction("Index", "Creative");
            }
        }
    }

    return View();
}

在 userlogoncontrol 我做了这样的改变。 但它没有显示用户名和我的垃圾箱。

<%
    if (HttpContext.Current.User.Identity.IsAuthenticated)
    {
%>
        Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
        [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ] |

<%
        if(HttpContext.Current.User.IsInRole("Brand"))
        {
%>
        [ <%= Html.ActionLink("my bin", "Bin", "Brand") %> ] |
<%
        }
        else if (HttpContext.Current.User.IsInRole("Creative"))
        {
%>
         [ <%= Html.ActionLink("my bin", "Bin", "Creative") %> ] |
<%
        }
    }
    else 
    {
%> 
        [ <%= Html.ActionLink("Log On", "LogOn", "Account") %> ]
<%
    }
%>

我错过了什么吗?如何在 cookie 中保存我的用户详细信息,例如用户 ID 和角色。

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    您链接到的文章是关于 WebForms 的。在 ASP.NET MVC 中,我建议您使用自定义 [Authorize] 过滤器。你的Login 看起来不错。你可以保持这样。然后写一个自定义的Authorize属性:

    public class MyAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var isAuthorized = base.AuthorizeCore(httpContext);
            if (isAuthorized)
            {
                var cookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
                if (cookie != null)
                {
                    var ticket = FormsAuthentication.Decrypt(cookie.Value);
                    var roles = ticket.UserData.Split(',');
                    var identity = new GenericIdentity(ticket.Name);
                    httpContext.User = new GenericPrincipal(identity, roles);
                }
            }
            return isAuthorized;
        }
    }
    

    现在用这个自定义属性装饰一个控制器/动作:

    [MyAuthorize]
    public ActionResult Foo()
    {
        // here the this.User property will represent the custom principal
        ...
    }
    

    现在需要联系Global.asax

    【讨论】:

      【解决方案2】:

      _identity = Thread.CurrentPrincipal.Identity;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-10
        • 1970-01-01
        • 2019-09-20
        • 2023-03-03
        相关资源
        最近更新 更多