/// <summary> /// Applies a correctly setup principle to the Http request /// </summary> /// <param name="httpContext"></param> public void ApplyPrincipleToHttpRequest(HttpContext httpContext) { // declare a collection to hold roles for the current user String[] roles; // Get the current identity var identity = HttpContext.Current.User.Identity; // Check if the request is authenticated... if (httpContext.Request.IsAuthenticated) { // ...it is so load the roles collection for the user roles = Roles.GetRolesForUser(identity.Name); } else { // ...it isn't so load the collection with the unknown role roles = new[] { _configuration.UnknownUserRoleName }; } // Create a new WebIdenty from the current identity // and using the roles collection just populated var webIdentity = new WebIdentity(identity, roles); // Create a principal using the web identity and load it // with the app configuration var principal = new WebsitePrincipal(webIdentity) { ApplicationConfiguration = _configuration }; // Set the user for the specified Http context httpContext.User = principal; }
public int Login(Account account, Boolean isAutoLogin, Boolean isPasswordMd5, IContext context) { string userName = account.UserName; string password = account.Password; if (string.IsNullOrEmpty(userName.Trim())) { throw new Exception("用户名不能为空"); } password = isPasswordMd5 ? password.Trim().ToMd5() : password.Trim(); WebIdentity identity = GeneralIdentity(userName, password, context.Site.Score.LogOn); if (identity == null) { return(-1); //无账号 } Logout(context); DateTime expires = isAutoLogin ? DateTime.Now.AddMinutes(60) : DateTime.Now.AddYears(1); var authTicket = new FormsAuthenticationTicket(1, identity.Name, DateTime.Now, expires, true, JsonAdapter.Serialize(identity)); string encryptedTicket = FormsAuthentication.Encrypt(authTicket); var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) { Expires = expires }; context.HttpContext.Response.Cookies.Add(authCookie); if (!isAutoLogin) { return(identity.Status); } return(identity.Status); }
void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e) { HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { string encTicket = authCookie.Value; if (!String.IsNullOrEmpty(encTicket)) { FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket); WebIdentity id = new WebIdentity(ticket); GenericPrincipal prin = new GenericPrincipal(id, null); HttpContext.Current.User = prin; } } }
private static INotificationTagProxy GetBusinessLayerProxy(IPrincipal userPrincipal) { var p66Principal = WebIdentity.GetP66Principal(userPrincipal); return(new NotificationTagProxy(p66Principal)); }
private static IMessageProxy GetBusinessLayerProxy(IPrincipal userPrincipal) { var p66Principal = WebIdentity.GetP66Principal(userPrincipal); return(new MessageProxy(p66Principal)); }
private static ILocationLocatorProxy GetBusinessLayerProxy(IPrincipal userPrincipal) { var p66Principal = WebIdentity.GetP66Principal(userPrincipal); return(new LocationLocatorProxy(p66Principal)); }
private static IEventCategoryProxy GetBusinessLayerProxy(IPrincipal userPrincipal) { var p66Principal = WebIdentity.GetP66Principal(userPrincipal); return(new EventCategoryProxy(p66Principal)); }