public void ConstructorSetsUserId() { var identity = new TokenIdentity(1, "token"); Assert.AreEqual(1, identity.UserId, "The constructor should set the UserId"); }
public override void OnActionExecuted(HttpActionExecutedContext context) { TokenProvider tokenProvider = new TokenProvider(); TokenIdentity tokenIdentity = new TokenIdentity(); tokenIdentity.UserAgent = context.Request.Headers.UserAgent.ToString(); if (context.Request.Headers.Referrer != null) { tokenIdentity.IP = context.Request.Headers.Referrer.Authority; } if (context.Request.Headers.Contains("access_token")) { tokenIdentity.Token = context.Request.Headers.GetValues("access_token").FirstOrDefault(); } if (!tokenProvider.ValidateToken(ref tokenIdentity)) { context.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); } else { // context.User = new System.Security.Claims.ClaimsPrincipal(tokenIdentity); } base.OnActionExecuted(context); }
public void ConstructorSetsToken() { var identity = new TokenIdentity(1, "token"); Assert.AreEqual("token", identity.Token, "The constructor should set the Token"); }
public void NameIsUserId() { var identity = new TokenIdentity(1, "token"); Assert.AreEqual("1", identity.Name, "Name should be the string represenation of UserId"); }
public void IsAuthenticatedIsTrue() { var identity = new TokenIdentity(1, "token"); Assert.IsTrue(identity.IsAuthenticated, "IsAuthenticated should be true."); }
public void AuthenticationTypeIsToken() { var identity = new TokenIdentity(1, "token"); Assert.AreEqual("Token", identity.AuthenticationType, "AuthenticationType should be \"Token\""); }
public TokenEndpointBehavior(TokenIdentity identity) { Token = new AuthenticationToken() { Token = identity.Token, UserID = identity.UserId }; }
public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context) { var data = new TokenIdentity() { Token = context.AccessToken, UserId = context.Identity.Claims.Last().Value, Id = Guid.NewGuid().ToString() }; _service.SaveTokenIdentity(data); return(base.TokenEndpointResponse(context)); }
public void TypedAndLiteralAreMergedByTypedLiteral() { var target = new TokenCollection(); target.Add(TokenIdentity.Typed(-1, typeof(string))); Assert.AreEqual(1, target.Count); target.Add(TokenIdentity.Literal(-1, "foo")); Assert.AreEqual(2, target.Count); target.Add(TokenIdentity.TypedLiteral(-1, "foo", typeof(string))); Assert.AreEqual(1, target.Count); }
internal TokenCreateRequest BuildTokenCreateRequest() { TokenIdentity obo = new TokenIdentity(null, "testid"); TokenCreateRequest request = new TokenCreateRequest(obo) { MaxUsageCount = 1, ProtectedResource = bogusTestUrl, Context = JObject.Parse(@"{ ""x"":""value""}"), }; return(request); }
public JsonResult Login(LoginForm login) { using (SoHoaEntities db = new SoHoaEntities()) { S_Users user = db.S_Users.SingleOrDefault(x => x.UserName == login.Username); if (user != null) { string passwordSalt = user.PasswordSalt; string passwordInput = AuthenticationHelper.GetMd5Hash(passwordSalt + login.Password); string passwordUser = user.Password; if (passwordInput.Equals(passwordUser)) { TokenProvider tokenProvider = new TokenProvider(); TokenIdentity token = tokenProvider.GenerateToken(login.Username, Request.Headers["User-Agent"].ToString(), HttpContext.Request.UserHostAddress, Guid.NewGuid().ToString(), DateTime.Now.AddHours(7).Ticks); token.SetAuthenticationType("Custom"); token.SetIsAuthenticated(true); db.AccessTokens.Add(new AccessToken() { Token = token.Token, EffectiveTime = new DateTime(token.EffectiveTime), ExpiresIn = token.ExpiresTime, IP = token.IP, UserAgent = token.UserAgent, UserName = token.Name }); db.SaveChanges(); return(Json( new { Token = token, Profile = new { Username = token.UserName, FullName = user.UserName, }, User = new { UserName = user.UserName, UserId = user.UserID } })); } } } return(Json("Login failed!")); }
public IHttpActionResult Login(LoginForm loginForm) { using (ApplicationDbContext db = new ApplicationDbContext()) { Users user = db.Users.Include(x => x.LoaiTaiKhoan).SingleOrDefault(x => x.UserName == loginForm.Username); if (user != null) { string passwordSalt = user.PasswordSalt; string passwordInput = AuthenticationHelper.GetMd5Hash(passwordSalt + loginForm.Password); string passwordUser = user.Password; if (String.Equals(passwordInput, passwordUser, StringComparison.InvariantCulture) && user.Active == true) { TokenProvider tokenProvider = new TokenProvider(); TokenIdentity token = tokenProvider.GenerateToken(user.UserId, loginForm.Username, Request.Headers.UserAgent.ToString(), "", Guid.NewGuid().ToString(), DateTime.Now.Ticks); token.SetAuthenticationType("Custom"); token.SetIsAuthenticated(true); db.AccessTokens.Add(new AccessTokens() { Token = token.Token, EffectiveTime = new DateTime(token.EffectiveTime), ExpiresIn = token.ExpiresTime, IP = token.IP, UserAgent = token.UserAgent, UserName = token.Name }); db.SaveChanges(); return(Ok( new { AccessToken = token, Profile = new { UserId = user.UserId, Username = user.UserName, Email = user.Email, LoaiTaiKhoanID = user.LoaiTaiKhoanID, LoaiTaiKhoan = user.LoaiTaiKhoan.TenLoai, CoSoID = user.CoSoID } })); } } return(Ok("Login failed!")); } }
//private readonly string baseurl = "https://soistio.com/"; public async Task <TokenIdentity> LoginIdentityApi(string username, string password) { using (var client = new HttpClient()) { var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("username", username), new KeyValuePair <string, string>("password", password), new KeyValuePair <string, string>("grant_type", "password") }); var request = await client.PostAsync(baseurl + "Token", formContent); var content = await request.Content.ReadAsStringAsync(); TokenIdentity model = JsonConvert.DeserializeObject <TokenIdentity>(content); return(model); } }
protected bool GetToken(out int userId, out string token) { userId = -1; token = string.Empty; IPrincipal principal = HttpContext.User; if (principal != null) { TokenIdentity identity = principal.Identity as TokenIdentity; if (identity != null && identity.IsAuthenticated) { token = identity.Token; if (int.TryParse(identity.Name, out userId)) { return(true); } } } return(false); }
public static TokenIdentity GetUserFromToken(ClaimsIdentity identity) { var tokenIdentity = new TokenIdentity(); foreach (var claim in identity.Claims) { if (claim.Type.EndsWith("name")) { tokenIdentity.User = claim.Value; } if (claim.Type.EndsWith("nameidentifier")) { tokenIdentity.UserId = int.Parse(claim.Value); } if (claim.Type.EndsWith("provider")) { tokenIdentity.ProviderId = int.Parse(claim.Value); } } return tokenIdentity; }
public IHttpActionResult ValidateToken() { TokenIdentity tokenIdentity = ClaimsPrincipal.Current.Identity as TokenIdentity; return(Ok()); }
public TokenPrincipal(string name) { Identity = new TokenIdentity(name); }
private string GenerateTokenIdentity(string Id, string Email) { TokenIdentity identity = new TokenIdentity(); return(identity.GetToken()); }
public void SaveTokenIdentity(TokenIdentity tokenIdentity) { _uow.TokenIdentities.Create(tokenIdentity); _uow.TokenIdentities.Save(); }