[英]Unable to get the Current.User.Identity using formsauthentication
I am new to asp.net mvc 2. I'm learning mvc by doing a test project. 我是asp.net mvc 2的新手。我正在通过做一个测试项目来学习mvc。 I am using formsauthentication to log into my website.
我正在使用表单身份验证登录我的网站。 During login I am able to log into the website but I am not getting the useridentity.
登录期间,我可以登录该网站,但没有获得用户身份。
i have taken http://www.dotnetfunda.com/articles/article141.aspx as my reference website 我已将http://www.dotnetfunda.com/articles/article141.aspx用作我的参考网站
in my webconfig file. 在我的webconfig文件中。
<authentication mode="Forms">
<forms loginUrl="~/Home/login" defaultUrl="~/Home/login" cookieless="UseCookies" slidingExpiration="true" timeout="20" />
</authentication>
in login controller 在登录控制器中
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();
}
in userlogoncontrol i have made changes like this. 在userlogoncontrol中,我进行了这样的更改。 but it is not displaying username and my bin.
但它没有显示用户名和我的垃圾箱。
<%
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") %> ]
<%
}
%>
Did I miss anything?How can i save my user details like userid and role in cookie. 我错过了什么吗?如何在cookie中保存用户详细信息,例如用户ID和角色。
The article you have linked to is about WebForms. 您链接到的文章是关于WebForms的。 In ASP.NET MVC I would recommend you using a custom
[Authorize]
filter. 在ASP.NET MVC中,我建议您使用自定义的
[Authorize]
过滤器。 Your Login
looks fine. 您的
Login
看起来不错。 You can keep it like this. 您可以像这样保持它。 Then write a custom Authorize attribute:
然后编写一个自定义的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;
}
}
Now decorate a controller/action with this custom attribute: 现在使用此自定义属性装饰控制器/动作:
[MyAuthorize]
public ActionResult Foo()
{
// here the this.User property will represent the custom principal
...
}
Now need to touch at Global.asax
. 现在需要触摸
Global.asax
。
_identity = Thread.CurrentPrincipal.Identity;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.