正如你发布的代码一样,你可能会得到一些好的答案。我会尽量按照我的理解来回答,希望这能有所帮助。
-
您从哪里调用CustomIdentity?在Else部分的第一个方法中,我认为您可能需要使用
Not IsAuthenticated
如果您希望用户重定向到登录页面。大多数时候,如果票证无效,你甚至不必这么做——框架会为你这么做。
-
在
CustomIdentityUser
您有一个私有成员_Auth,在通过财产返回一些值时从未使用过它。您正在使用
Auth.UserName
而不是
_Auth.UserName
,我不确定这是如何工作的,除非UserName是静态成员。
-
为什么自定义身份依赖于
Auth
? 您可以通过contractor或公开public将所需的数据传递给自定义标识
setters
。为什么您需要身份验证内的身份验证票证?
-
这个
身份验证
课程有截止日期和其他不需要的东西。你可以有一个简单的
User
类来存储用户的基本详细信息。为什么要从的构造函数重定向用户
身份验证
?
-
这个
GetUserDetails
和
SetUserDataFromString
我不知道你为什么需要这些方法。这只是一个问题
Serializing
和
Deserializing
User类。
我知道您一定参考了一些博客来实现这种身份验证,但您有很大的空间来简化这一点。
阅读
this
邮递特别是如何实现自定义主体、如何设置authticket以及
PostAuthenticateRequest
方法
以下是一些可能有帮助的示例代码
interface ICustomPrincipal : IPrincipal
{
int UserId { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
}
public class CustomPrincipal : ICustomPrincipal
{
public CustomPrincipal()
{
}
public CustomPrincipal(string userName)
{
Identity = new GenericIdentity(userName);
}
public int UserId
{
get;
set;
}
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public IIdentity Identity
{
get;
private set;
}
public bool IsInRole(string role)
{
return false;
}
}
public class User
{
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
public static class FormsAuthHelper
{
public static void SetAuthTicket(User user, HttpContextBase context)
{
var serializer = new JavaScriptSerializer();
var userData = serializer.Serialize(user);
var authTicket = new FormsAuthenticationTicket(
1, user.UserName,
DateTime.Now, DateTime.Now.AddMinutes(30),
false, userData);
var ticket = FormsAuthentication.Encrypt(authTicket);
var faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticket);
context.Response.Cookies.Add(faCookie);
}
public static void Logout()
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
public static CustomPrincipal GetPrincipal(User user)
{
return new CustomPrincipal(user.UserName) { FirstName = user.FirstName, LastName = user.LastName, UserId = user.EntityId };
}
}
后身份验证请求事件如下
protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
var authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == string.Empty)
return;
try
{
var ticket = FormsAuthentication.Decrypt(authCookie.Value);
var serializer = new JavaScriptSerializer();
var user = serializer.Deserialize<User>(ticket.UserData);
var newUser = FormsAuthHelper.GetPrincipal(user);
HttpContext.Current.User = newUser;
}
catch
{
//do nothing
}
}
最后,当用户登录时
public ActionResult Login(LoginModel loginModel)
{
if (ModelState.IsValid)
{
var user = _userRepository.Get(x => x.UserName == loginModel.UserName).SingleOrDefault();
if (user != null && PasswordHash.ValidatePassword(loginModel.Password, user.Password))
{
FormsAuthHelper.SetAuthTicket(user, HttpContext);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("NotFound", "User not found");
}
return View(loginModel);
}