public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); AccesoSistema user; try { using (AccesoSistemaRepository _repo = new AccesoSistemaRepository()) { user = await _repo.FindUser(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "Usuario o contraseƱa incorrecta"); return; } } } catch (Exception e) { context.SetError(e.Message); return; } var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("userName", context.UserName)); //TODO logger identity.AddClaim(new Claim("id", user.AccesoID.ToString())); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("role", "user")); identity.AddClaim(new Claim("ip", this.GetIPServerVariables())); context.Validated(identity); var p = new Personas(); p.ClavePersona = user.ClavePersona; p.Nombre = user.UserName; SimpleSessionPersister.PersonaId = p.ClavePersona; //HttpContext.Current.User.Identity.Name; SimpleSessionPersister.nombreUsuario = user.UserName; BitacoraSISTEMA.InsertaLogin(p, true, this.GetIP()); }
public AccesoSistemaController() { _repository = new AccesoSistemaRepository(); }