4
Answers

Add Authentication to Web API

ikrami sami

ikrami sami

7y
263
1
I need to add authentication to my Web API, so user must add token, or password to access the Web API.
 
Under Controller folder i have :
 
[AcceptVerbs("GET")]
public Reservation RPCStyleMethodFetchFirstEmployees()
{
return listEmp.FirstOrDefault();
}
// Get All Reservations
[HttpGet]
[ActionName("GetReservation")]
//public Reservation Get()
public List<Reservation> Get()
{
string source = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection conn = new SqlConnection(source);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string sql = "Select s.Id,a.Room_No ,s.Room_Description,s.Room_Capacity, b.Description, s.Location,s.Start_Date,s.Start_Time,CONVERT(VARCHAR(20),CONVERT(Varchar(50),( Concat(CONVERT(VARCHAR, s.Start_Date, 126),'T',CONVERT(VARCHAR, s.End_Time, 127)) ),105),131)+'.00Z',s.End_Time,s.Meeting_Title,s.Reservation_Reason, s.Status,s.Attendance,s.Remarks,s.Mail_To,s.Mail_CC, s.Created_User, s.Created_Password, s.Created_Access_Key from Reservation s , Rooms a, Room_Type b Where s.Room_No = a.ID And s.Room_Type = b.ID";
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
Reservation emp = null;
conn.Open();
reader = cmd.ExecuteReader();
List<Reservation> emps = new List<Reservation>();
while (reader.Read())
{
//read data
emp = new Reservation();
emp.Id = Convert.ToInt32(reader.GetValue(0));
emp.Room_No = reader.GetValue(1).ToString();
emp.Room_Description = reader.GetValue(2) as string;//reader.GetValue(3).ToString();
emp.Room_Capacity = (reader.GetValue(3) as int?) ?? 0; //Convert.ToInt32(reader.GetValue(4));
emp.Room_Type = reader.GetValue(4) as string;//reader.GetValue(5).ToString();
emp.Location = reader.GetValue(5) as string; //reader.GetValue(6).ToString();
//emp.Start_Date = reader.GetValue(6) as string; //reader.GetValue(7).ToString();
emp.Start_Date = reader.GetValue(6).ToString();
emp.Start_Time = reader.GetValue(7).ToString();
emp.Start_Date_Time = reader.GetValue(8).ToString();
emp.End_Time = reader.GetValue(9).ToString();
emp.Meeting_Title = reader.GetValue(10) as string; //reader.GetValue(10).ToString();
emp.Reservation_Reason = reader.GetValue(11) as string; //reader.GetValue(11).ToString();
emp.Status = reader.GetValue(12) as string; //reader.GetValue(12).ToString();
emp.Attendance = (reader.GetValue(13) as int?) ?? 0;//Convert.ToInt32(reader.GetValue(13));
emp.Remarks = reader.GetValue(14) as string;//reader.GetValue(14).ToString();
emp.Mail_To = reader.GetValue(15) as string;//reader.GetValue(15).ToString();
emp.Mail_CC = reader.GetValue(16) as string;//reader.GetValue(16).ToString();
emp.Created_User = reader.GetValue(17) as string;//reader.GetValue(16).ToString();
emp.Created_Password = reader.GetValue(18) as string;//reader.GetValue(16).ToString();
emp.Created_Access_Key = reader.GetValue(19) as string;//reader.GetValue(16).ToString();
emps.Add(emp);
}
conn.Close();
return emps;
}
and under Models i have  
public class Reservation
{
public int Id { get; set; }
public string Room_No { get; set; }
public string Room_Description { get; set; }
public int Room_Capacity { get; set; }
public string Room_Type { get; set; }
public string Location { get; set; }
public string Start_Date { get; set; }
public string Start_Time { get; set; }
public string Start_Date_Time { get; set; }
public string End_Time { get; set; }
public string Meeting_Title { get; set; }
public string Reservation_Reason { get; set; }
public string Status { get; set; }
public int Attendance { get; set; }
public string Remarks { get; set; }
public string Mail_To { get; set; }
public string Mail_CC { get; set; }
public string Created_User { get; set; }
public string Created_Password { get; set; }
public string Created_Access_Key { get; set; }
// public string FirstName { get; set; }
// public string LastName { get; set; }
// public string Department { get; set; }
//}
}
 
How to add Authentication before they start get the data 
Answers (4)