public IDandToken Registration(ClientDto userdto) { if (ModelState.IsValid == false) { return(new IDandToken()); } IdentityResult result = repos.CreateClient(userdto); if (result.Succeeded) { using (HttpClient httpClient = new HttpClient()) { Dictionary <string, string> tokenDetails = null; // var messageDetails = new Message { Id = 4, Message1 = des }; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:4700/"); var login = new Dictionary <string, string> { { "grant_type", "password" }, { "username", userdto.Email }, { "password", userdto.Password }, }; var response = client.PostAsync("Token", new FormUrlEncodedContent(login)).Result; if (response.IsSuccessStatusCode) { tokenDetails = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content.ReadAsStringAsync().Result); if (tokenDetails != null && tokenDetails.Any()) { var tokenNo = tokenDetails.FirstOrDefault().Value; IdentityUser user = repos.Find(userdto.Email, userdto.Password); Client newclient = new Client { NationalID = userdto.NationalID, ClientName = userdto.Name, UserID = user.Id }; context.Clients.Add(newclient); context.SaveChanges(); return(new IDandToken { ID = user.Id, Token = tokenDetails.FirstOrDefault().Value }); } } } } return(new IDandToken()); }
public IHttpActionResult RegistrationDriver(ApplicantDto userdto) { AuthBL repos = new AuthBL(); if (ModelState.IsValid == false) { return(BadRequest()); } IdentityResult result = repos.CreateDriver(userdto); if (result.Succeeded) { using (HttpClient httpClient = new HttpClient()) { Dictionary <string, string> tokenDetails = null; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:4700/"); var login = new Dictionary <string, string> { { "grant_type", "password" }, { "username", userdto.Email }, { "password", userdto.Password }, }; var response = client.PostAsync("Token", new FormUrlEncodedContent(login)).Result; if (response.IsSuccessStatusCode) { tokenDetails = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content.ReadAsStringAsync().Result); if (tokenDetails != null && tokenDetails.Any()) { var tokenNo = tokenDetails.FirstOrDefault().Value; IdentityUser user = repos.Find(userdto.Email, userdto.Password); AdminController applicantobj = new AdminController(); NewApplicant applicant = applicantobj.getApplicant(userdto.NationalID); Driver driver = new Driver { UserID = user.Id, NationalID = applicant.NationalID, Rate = 0, numberOfTrips = 0, AvgRate = 0.0, }; applicant.Status = NewApplicantstatus.Accepted; context.Entry(applicant).State = EntityState.Modified; context.SaveChanges(); context.Drivers.Add(driver); context.SaveChanges(); return(Ok()); } } } } return(BadRequest()); }
public override async Task GrantResourceOwnerCredentials( OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); AuthBL repo = new AuthBL(); IdentityUser user = repo.Find(context.UserName, context.Password); if (user == null) { context.SetError("Error User Pass Not valid"); } ClaimsIdentity claims = new ClaimsIdentity(context.Options.AuthenticationType); claims.AddClaim(new Claim("Name", user.UserName)); claims.AddClaim(new Claim(ClaimTypes.Role, repo.getRole(user))); //claims.AddClaim(new Claim(ClaimTypes.Role, "Admin")); //claims.AddClaim(new Claim(ClaimTypes.Role, "Driver")); //claims.AddClaim(new Claim(ClaimTypes.Role, "Client")); //claims.AddClaim(new Claim("Role", "Admin")); //claims.AddClaim(new Claim("Role", "User")); context.Validated(claims); //check user usin gLAyer //create toke //error //context.UserName; //context.Password }
//public override async Task GrantResourceOwnerCredentials( // OAuthGrantResourceOwnerCredentialsContext context) //{ // context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); // AuthBL repo = new AuthBL(); // IdentityUser user = repo.Find(context.UserName, context.Password); // if (user == null) // { // context.SetError("Error User Pass Not valid"); // } // ClaimsIdentity claims = new ClaimsIdentity(context.Options.AuthenticationType); // claims.AddClaim(new Claim("Name", user.UserName)); // claims.AddClaim(new Claim("Role", "Admin")); // context.Validated(claims); //} public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); AuthBL repo = new AuthBL(); IdentityUser user = repo.Find(context.UserName, context.Password); var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim("Name", context.UserName)); identity.AddClaim(new Claim(ClaimTypes.Role, "User")); // identity.AddClaim(new Claim("Name", user.UserName)); identity.AddClaim(new Claim("Role", "Admin")); context.Validated(identity); }
public IDandToken Login(string name, string password) { if (ModelState.IsValid == false) { return(new IDandToken()); } IdentityUser result = repos.Find(name, password); if (result != null && repos.getRole(result).Contains("Driver")) { using (HttpClient httpClient = new HttpClient()) { Dictionary <string, string> tokenDetails = null; // var messageDetails = new Message { Id = 4, Message1 = des }; HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://localhost:4700/"); var login = new Dictionary <string, string> { { "grant_type", "password" }, { "username", name }, { "password", password }, }; var response = client.PostAsync("Token", new FormUrlEncodedContent(login)).Result; if (response.IsSuccessStatusCode) { tokenDetails = JsonConvert.DeserializeObject <Dictionary <string, string> >(response.Content.ReadAsStringAsync().Result); if (tokenDetails != null && tokenDetails.Any()) { var tokenNo = tokenDetails.FirstOrDefault().Value; return(new IDandToken { ID = result.Id, Token = tokenDetails.FirstOrDefault().Value }); } } } } return(new IDandToken()); }