「彼のタイプを保存する」とはどういう意味ですか? 彼の役割ということですか?では、基本的にユーザーはアプリケーション内でどのような役割を果たしているのでしょうか? それがロールの場合は、おそらく Authcookie に保存するのが適切な場所です。認証 Cookie に追加の値を追加したり、追加の値を考慮した独自の Authorize Attribute をロールしたりすることもできます。これにより、ユーザー プリンシパル オブジェクトで使用できるようになります。設定; } 文字列 FirstName { get; 設定; } 文字列 LastName { get; 設定; } 文字列 EmailAddress { get; 設定; } Guid CompanyID { get; 設定; } }
public class CustomPrincipal : ICustomPrincipal
{
public IIdentity Identity { get; private set; }
public bool IsInRole(string role)
{
return false;
}
public CustomPrincipal()
{
}
public CustomPrincipal(IIdentity indentity)
{
this.Identity = new GenericIdentity(indentity.Name);
}
public CustomPrincipal(string email)
{
this.Identity = new GenericIdentity(email);
}
public Guid UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public Guid CompanyID { get; set; }
public string CompanyName { get; set; }
public string JobTitle { get; set; }
}`.
public sealed class CustomAuthoriseAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized) return false;
CustomPrincipal customPrincipal = null;
HttpCookie authCookie = httpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
var serializer = new JavaScriptSerializer();
if (authTicket != null)
{
var serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);
customPrincipal = new CustomPrincipal(authTicket.Name)
{
UserID = serializeModel.UserID,
FirstName = serializeModel.FirstName,
LastName = serializeModel.LastName,
CompanyID = serializeModel.CompanyID,
EmailAddress = serializeModel.EmailAddress,
CompanyName = serializeModel.CompanyName,
JobTitle = serializeModel.JobTitle,
};
}
}
HttpContext.Current.User = customPrincipal;
return isAuthorized;
}
}
public class CustomPrincipalSerializeModel
{
public Guid UserID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public Guid CompanyID { get; set; }
public string CompanyName { get; set; }
public string JobTitle { get; set; }
}
次に、ログイン方法は次のようになります
if (!membershipService.IsAccountLockedOut(loginModel.Email) &&
membershipService.Login(loginModel.Email, loginModel.Password))
{
UserDto user = membershipService.GetUserDetail(loginModel.Email);
var cookieContext = new CookieContext();
cookieContext.SetAuthenticationToken(user);
//Need to check if user has reset thier password and needs to change it
if (!user.PasswordReset)
{
return RedirectToLocal(returnUrl);
}
else
{
return RedirectToAction("ChangePassword", "Account");
}
}
Set Authentication Method は次のようになります。
public void SetAuthenticationToken(UserDto userDto)
{
string userData;
string encTicket;
var serializeModel = new CustomPrincipalSerializeModel();
serializeModel.UserID = userDto.ID;
serializeModel.FirstName = userDto.FirstName;
serializeModel.LastName = userDto.LastName;
serializeModel.EmailAddress = userDto.Email;
serializeModel.CompanyID = userDto.CompanyID;
serializeModel.CompanyName = userDto.Company;
serializeModel.JobTitle = userDto.JobTitle;
var serializer = new JavaScriptSerializer();
userData = serializer.Serialize(serializeModel);
var autTicket = new FormsAuthenticationTicket(1, userDto.Email, DateTime.Now,
DateTime.Now.AddMinutes(15), false, userData);
encTicket = FormsAuthentication.Encrypt(autTicket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
cookie.HttpOnly = true;
HttpContext.Current.Response.Cookies.Add(cookie);
}
アプリケーション全体でユーザーと一緒に移動する必要があるすべてのデータは、認証 Cookie で利用でき、CustomAuthorise 属性を使用するときはいつでも User オブジェクトで利用できます。
[CustomAuthorise]
[OutputCache(NoStore = true, VaryByParam = "*", Duration = 0)]
public ActionResult Index()
{
var model = _someService.SomeFunction(User.CompanyID); //Company ID is from Auth Cookie
return View(model);
}