public void SA_Authenticate_Success() { DateTime expireTime = DateTime.Now.AddDays(1); //生成授权码 string authenticationCode = SecurityAuthentication.Encrypt(_checkCode, expireTime, 0, _publicKey); //认证 AuthorizationResult result = SecurityAuthentication.Authenticate(authenticationCode, _privateKey, _checkCode); Assert.IsTrue(result.Success, "未过期,结果应该是认证成功!"); }
public void SA_SA_Authenticate_FailureByExpireTime() { DateTime expireTime = new DateTime(2017, 8, 9); //生成授权码 string authenticationCode = SecurityAuthentication.Encrypt(_checkCode, expireTime, 0, _publicKey); //认证 AuthorizationResult result = SecurityAuthentication.Authenticate(authenticationCode, _privateKey, _checkCode); Assert.IsTrue(!result.Success, "当前时间超过授权期限,验证失败!"); Assert.IsTrue(result.AuthorizationState == AuthorizationState.Expire, "验证失败原因:过期!"); }
public void SA_SA_Authenticate_FailureByAuthCode() { DateTime expireTime = DateTime.Now.AddDays(1); //生成授权码 string authenticationCode = SecurityAuthentication.Encrypt(_checkCode, expireTime, 0, _publicKey); string errorAuthCode = "111111111111"; //认证 AuthorizationResult result = SecurityAuthentication.Authenticate(errorAuthCode, _privateKey, _checkCode); Assert.IsTrue(!result.Success, "授权码错误,验证失败!"); }
public void SA_SA_Authenticate_FailureByPublicKey() { DateTime expireTime = DateTime.Now.AddDays(1); //生成授权码 string authenticationCode = SecurityAuthentication.Encrypt(_checkCode, expireTime, 0, _publicKey); string errorPrivateKey = "<RSAKeyValue><Modulus>nr7rq0sgR0GokC/dTajW0MzTF1KJgeAhyxgMUhylsLcJVHqY4oo2SHs6uDYydfPd4m7t5uaaLmYdXTUfXDz9HNx9YwnuwDWy9GuNy7T9+ONENk/0hlfDs0bJKYgjcycu//QziY6WJi7yBZoTVSNmzj0takyoNqgSKLWhB20yTPk=</Modulus><Exponent>ABCD</Exponent></RSAKeyValue>"; //认证 AuthorizationResult result = SecurityAuthentication.Authenticate(authenticationCode, errorPrivateKey, _checkCode); Assert.IsTrue(!result.Success, "公钥错误,验证失败!"); }
public void SA_SA_Authenticate_FailureByCheckCode() { DateTime expireTime = DateTime.Now.AddDays(1); string errorCheckCode = "错误的校验码"; //生成授权码 string authenticationCode = SecurityAuthentication.Encrypt(_checkCode, expireTime, 0, _publicKey); //认证 AuthorizationResult result = SecurityAuthentication.Authenticate(authenticationCode, _privateKey, errorCheckCode); Assert.IsTrue(!result.Success, "校验码错误,验证失败!"); Assert.IsTrue(result.AuthorizationState == AuthorizationState.CheckCodeError, "验证失败原因:校验码错误!"); }
/// <summary> /// 生成 License Code。 /// </summary> /// <param name="entity"></param> /// <returns></returns> internal static string GeneratorLicenseCode(LicenseEntity entity) { if (entity.LicenseTarget == LicenseTarget.None) { MessageBox.Show(LicenseManagerResource.ManagerFormGetLicenseEntityAuthenticationTargetWarning, LicenseManagerResource.ManagerFormValidateParametersWarning, MessageBoxButtons.OK, MessageBoxIcon.Warning); return(string.Empty); } var authCode = new AuthorizationCode { ExpireTime = entity.ExpireTime, Mac = entity.MacCode, Category = entity.LicenseTarget == LicenseTarget.Development ? 0 : 1 }; var licenseCode = SecurityAuthentication.Encrypt(authCode, LicenseManagerResource.PublicKey); return(licenseCode); }
public ActionResult Login(LoginRequest request, string returnUrl) { if (ModelState.IsValid) { //if (ValidateUser(model.UserName, model.Password)) if (request.LoginName.ToLower() == "admin") { AuthenticationUserInfo authUserInfo = new AuthenticationUserInfo()//GetuserInfo(model.UserName); { ID = "123456789", UID = "", UserName = "******", Roles = "", }; //if (userInfo.RoleName.ToLower() == "admin") //{ // role = "Admin"; //} SecurityAuthentication.SetFormsAuthenticationTicket <AuthenticationUserInfo>(authUserInfo.ID, authUserInfo, TimeSpan.FromMinutes(20), true); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return(new RedirectResult(returnUrl)); } else { return(new RedirectResult("~/")); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } return(View(request)); }
public ActionResult Header() { AuthenticationUserInfo authUserInfo = SecurityAuthentication.GetFormsAuthenticationTicket <AuthenticationUserInfo>("authUserInfo.ID"); IEnumerable <MenuInfoViewModel> mainMenuList = new List <MenuInfoViewModel>() { new MenuInfoViewModel() { MenuName = "动态信息" }, new MenuInfoViewModel() { MenuName = "用户管理" }, new MenuInfoViewModel() { MenuName = "商品管理" }, new MenuInfoViewModel() { MenuName = "订单管理" }, new MenuInfoViewModel() { MenuName = "报表管理" }, new MenuInfoViewModel() { MenuName = "系统管理" } }; return(View(Tuple.Create( authUserInfo, //Item1:AuthUserInfo mainMenuList //Item2:MemuInfoList ))); }
protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext.User.Identity.IsAuthenticated) { AuthenticationUserInfo authUserInfo = SecurityAuthentication.GetFormsAuthenticationTicket <AuthenticationUserInfo>("authUserInfo.ID"); if (authUserInfo == null) { return(false); } #region mm // ApplicationSignInManager SignInManager = httpContext.GetOwinContext().Get<ApplicationSignInManager>(); // ApplicationUserManager UserManager = httpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); // string appid = string.Empty; // string secret = string.Empty; // appid = WxPayConfig.APPID; // secret = WxPayConfig.APPSECRET; // var code = httpContext.Request["Code"]; // string returnUrl = System.Web.HttpUtility.UrlDecode(httpContext.Request["ReturnUrl"] ?? "/"); // if (string.IsNullOrEmpty(code)) // { // string host = httpContext.Request.Url.Host; // string path = httpContext.Request.Path; // string redirectUrl = "http://" + host + path + "?ReturnUrl=" + System.Web.HttpUtility.UrlEncode(returnUrl);//重定向的url,这里不需要进行编码,在后面会自己编码 // try // { // //todo:通过微信获取2.0授权的url // string url = Wechat.AuthorizeUrl(appid, redirectUrl, "state", "snsapi_base"); // httpContext.Response.Redirect(url); // } // catch (System.Exception ex) // { //#if DEBUG // httpContext.Response.Write("构造网页授权获取code的URL时出错,错误是:" + ex.Message); // httpContext.Response.End(); //#endif // } // } // else // { // var client = new System.Net.WebClient(); // client.Encoding = System.Text.Encoding.UTF8; // string url = Wechat.AccessTokenUrl(appid, secret, code); // var data = client.DownloadString(url); // var obj = JsonConvert.DeserializeObject<Dictionary<string, string>>(data); // string accessToken; // if (!obj.TryGetValue("access_token", out accessToken)) // { //#if DEBUG // httpContext.Response.Write("构造网页授权获取access_token的URL时出错"); // httpContext.Response.End(); //#endif // } // var openid = obj["openid"]; // Utils.WidgetCode.ServerInfo.SetCookies("WXopenid", openid, DateTime.MinValue); // var existUser = UserManager.Users.FirstOrDefault(p => p.OpenId == openid); // if (existUser != null) // { // SignInManager.SignInAsync(existUser, false, false); // httpContext.Response.Redirect(returnUrl); // } // } #endregion return(true); } return(false); }