C# 如果我正在验证用户权限-自定义主体,我需要修改代码
在我的登录方法中,我使用以下代码登录用户:C# 如果我正在验证用户权限-自定义主体,我需要修改代码,c#,asp.net-mvc,asp.net-mvc-3,forms-authentication,iprincipal,C#,Asp.net Mvc,Asp.net Mvc 3,Forms Authentication,Iprincipal,在我的登录方法中,我使用以下代码登录用户: FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 因为我总是想在需要用户ID和其他一些数据时避免数据库调用,所以我做了一些事情,但我不太确定我是否做对了。 所以这就是为什么我需要有人检查我的代码,它是否安全且不愚蠢:) 我这样做是否违反了一些规则。 这是我网站安全阶段的最后阶段,因为我不再使用会员资格/角色。 所以我在SetAuth上面改了。。。代码为: Cus
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
因为我总是想在需要用户ID
和其他一些数据时避免数据库调用,所以我做了一些事情,但我不太确定我是否做对了。所以这就是为什么我需要有人检查我的代码,它是否安全且不愚蠢:)
我这样做是否违反了一些规则。
这是我网站安全阶段的最后阶段,因为我不再使用会员资格/角色。
所以我在SetAuth上面改了。。。代码为:
CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
var usr = userRepository.GetUser(model.UserName);
serializeModel.UserId = usr.UserId;
serializeModel.Username = usr.UserName;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string userData = serializer.Serialize(serializeModel);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,
usr.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
model.RememberMe,
userData);
string encTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
Response.Cookies.Add(faCookie);
在我的Global.asax中,我添加了:
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
JavaScriptSerializer serializer = new JavaScriptSerializer();
CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);
CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
newUser.UserId = serializeModel.UserId;
newUser.Username = serializeModel.Username;
newUser.FirstName = serializeModel.FirstName;
newUser.LastName = serializeModel.LastName;
HttpContext.Current.User = newUser;
}
}
我看到的代码的第一个问题是,您应该使用web.config中的表单身份验证设置(例如超时、域、路径、requireSSL等)为表单身份验证票证和cookie设置这些值。现在您已经硬编码了这些值。例如,您为票据硬编码了30分钟超时,这可能不同于您的web.config中为cookie生命设置的超时。默认值为20分钟。但是,如果您在web.config中更改此值以增加它,您的cookie将比表单身份验证票证的寿命更长。因此,用户总是在30分钟后注销,而不是在您指定的超时之后
此外,我还将使用自定义Authorize属性来解析表单身份验证票证并设置主体,而不是使用全局
应用程序\u PostAuthenticateRequest
事件。第一个是实现这一目标的更有效的方法。但这只是一个建议,从安全性或行为角度来看没有问题。是的,我肯定会将这个值放在web.config中。我主要关心的是,我是否必须始终使用进行身份验证。SetAuthCookie()
,或者这种方式也是合法的。这是绝对好的。唯一的区别是FormsAuthentication.SetAuthCookie()
使用web.config中的值,而在您的代码中,您忘记了这样做,因为您手动添加了cookie。此外,我将使用自定义授权属性来解析表单身份验证票证并设置主体,而不是使用全局应用程序\u PostAuthenticateRequest事件。怎么用?你能详细说明一下吗@达林·迪米特罗夫
public interface ICustomPrincipal : IPrincipal
{
int UserId { get; set; }
string Username { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
}
public class CustomPrincipal : ICustomPrincipal
{
public IIdentity Identity { get; private set; }
public bool IsInRole(string role) { return false; }
public CustomPrincipal(string email)
{
this.Identity = new GenericIdentity(email);
}
public int UserId { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class CustomPrincipalSerializeModel
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
}