이 코드를 가이드로 사용하여 우리 자신의 모델과 데이터베이스를 사용하여 poeple을 로그 아웃합니다. IdentitySession 클래스는 컨트롤러에서 사인 인 된 사용자 데이터를 가져올 수 있습니다. 나는 약간을 여기에서 잘라서 우리 코드를 단순화하려고 노력했다. 그래서 이것이 단지 달릴 것을 기대하지 말아라. 희망이 도움이됩니다.
public ActionResult Login(int pageId) {
ViewData["ReturnUrl"] = Request["ReturnUrl"];
return View(Cms3Configuration.DefaultViewModelWithPage(attachedPage));
}
public ActionResult Process(int pageId, string login, string password, string ReturnUrl) {
var user = userRepository.GetByUserName(login);
ViewData["ReturnUrl"] = ReturnUrl;
if (user != null && authenticator.VerifyAccount(user, password)) {
authenticator.SignIn(user);
if (ReturnUrl.IsNotEmpty()) {
return Redirect(ReturnUrl);
}
return Redirect("~" + attachedPage.Parent.Url);
}
////login failed
TempData[TempDataKeys.Error] = "Invalid login";
return RedirectToAction("Login", new { pageId = pageId, ReturnUrl });
}
public ActionResult Logout(int pageId) {
authenticator.SignOut();
return RedirectToAction<LoginController>(x => x.Login(pageId), new {pageId = pageId});
}
public interface IAuthenticator {
void SignIn(User person);
IIdentity GetActiveIdentity();
WindowsPrincipal GetActiveUser();
void SignOut();
bool VerifyAccount(User person, string password);
bool HasRole(string person, string role);
}
public class Authenticator : IAuthenticator {
private readonly IHttpContextProvider _httpContextProvider;
private readonly ICryptographer _cryptographer;
private readonly IRepository repository;
public Authenticator(IHttpContextProvider httpContextProvider, ICryptographer cryptographer, IRepository repository) {
_cryptographer = cryptographer;
this.repository = repository;
_httpContextProvider = httpContextProvider;
}
public void SignIn(User user) {
FormsAuthentication.SignOut();
if (user == null)
return;
DateTime issued = DateTime.Now;
DateTime expires = issued.AddMinutes(30);
if (user.ExpiryDate.HasValue) {
if (user.Expires && expires > user.ExpiryDate)
expires = (DateTime) user.ExpiryDate;
}
var roles = user.Roles.Select(x => x.Name).ToList();
var ticket = new FormsAuthenticationTicket(1, user.UserName, issued, expires, false, string.Join(",", roles.Distinct().ToArray()));
var encryptedTicket = FormsAuthentication.Encrypt(ticket);
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { Expires = ticket.Expiration };
_httpContextProvider.GetCurrentHttpContext().Response.Cookies.Add(authCookie);
}
public IIdentity GetActiveIdentity() {
var httpcontext = _httpContextProvider.GetCurrentHttpContext();
if (httpcontext == null || httpcontext.User == null)
return null;
return httpcontext.User.Identity;
}
public WindowsPrincipal GetActiveUser() {
return _httpContextProvider.GetCurrentHttpContext().User as WindowsPrincipal;
}
public void SignOut() {
FormsAuthentication.SignOut();
}
public bool VerifyAccount(User person, string password) {
string passwordHash = _cryptographer.HashPassword(password, person.PasswordSalt);
return passwordHash == person.Password && !person.HasExpired() && person.Approved == true;
}
}
public interface IIdentitySession<T> {
T GetLoggedInIdentity();
bool IsAuthenticated { get; }
bool IsAdministrator { get; }
}
public class IdentitySession<T> : IIdentitySession<T> where T : Identity {
private readonly IAuthenticator<T> authenticator;
private readonly IRepository repository;
private readonly IHttpContextProvider httpContextProvider;
private T currentIdentity;
private static readonly object _lock = new object();
public IdentitySession(IAuthenticator<T> authenticator, IRepository repository,
IHttpContextProvider httpContextProvider) {
this.authenticator = authenticator;
this.activeDirectoryMapper = activeDirectoryMapper;
this.repository = repository;
this.httpContextProvider = httpContextProvider;
}
public virtual T GetLoggedInIdentity() {
IIdentity identity = authenticator.GetActiveIdentity();
if (identity == null)
return null;
if (!identity.IsAuthenticated)
return null;
lock (_lock) {
if (currentIdentity == null) {
currentIdentity = repository.Query<T>().Where(x => x.UserName == identity.Name).FirstOrDefault();
}
}
return currentIdentity;
}
public bool IsAuthenticated {
get { return httpContextProvider.GetCurrentHttpContext().User.Identity.IsAuthenticated; }
}
public bool IsAdministrator {
get { return false; }
}
}
Ok ... 맞춤 멤버십 제공 업체를 만들었습니다 ... 이제 어떻게해야합니까? –
@Shaul - web.config를 수정하여 기본값 대신 사용자 정의 멤버십 공급자를 사용합니다. –
좋습니다 ... 점점 가까워지고 있습니다. 이제 회원 공급자에게 사용자가 인증되었음을 알리는 방법은 무엇입니까? –