Skip to content
Loading
  • Bikash Sahu
    +1
    public HttpResponseMessage PostUser(User user)
    {
    Data = repository.GetAlluser.select(c => new { Email=c.Email,UserName= c.UserName })
    var response = Request.CreateResponse(HttpStatusCode.Created, Data);
    string uri = Url.Link("DefaultApi", new { id = user.Id });
    response.Headers.Location = new Uri(uri);
    return response;
    }
     
    By returning an HttpResponseMessage instead of a User, we can control the details of the HTTP response message, including the status code and the Location header.
    +1
  • Divyang Desai
    +1
    Without looking at code, it's hard to suggest you anything. Also, It really doesn't matter that you are using LINQ or Not. The problem is " you don't want to expose all of your database fields in return response of login".
    You should have full access of what to return and what to not. In following example you can see how you can return only two fields. IT IS NOT FULL CODE, but it's just for understanding purpose.
    1. var user = _userService.Login(username, password); // or linq query to fatch the da  
    2.   
    3. if(user == null)  
    4. return NotFound();  

    5. return Ok(new {userName = user.userName, email = user.Email });  
    Second way, you can create your DTO classes, and map it using library like AutoMapper.
    +1
  • Madan Shekar
    +1
    sql query in your controller
    string username="";
    string emailid="";
    public void getUser(string emailaddress,string password string dbcon)
    {
    string query = string.Empty;
    query = "select * from vtuser where emailaddress=@emailaddress and password=@password";
    if (IsSSO)
    {
    query = "select * from table name where loginname=@emailaddress";
    }
    SqlCommand cmd = new SqlCommand(query);
    cmd.Parameters.AddWithValue("@emailaddress", emailaddress);
    cmd.Parameters.AddWithValue("@password ", password
    DataTable dt = sqlExceute(cmd, dbcon);
    User user = new User();
    if (dt.Rows.Count > 0)
    {
    foreach (DataRow rows in dt.Rows)
    {
    if (rows["username"] != DBNull.Value)
    {
    username = Convert.ToString(rows["username"]);
    }
    if (rows["userid"] != DBNull.Value)
    {
    emailid = Convert.ToString(rows["email"]);
    }
    }
    }
    }
    public static DataTable sqlExceute(SqlCommand cmd, string dbcon)
    {
    Trace.TraceInformation("Inside sqlExecute");
    SqlConnection Con = new SqlConnection(dbcon);
    DataTable dT = new DataTable();
    int retryCount = 0;
    cmd.Connection = Con;
    retry:
    try
    {
    Trace.TraceInformation("before sqlExecute-retryCount-" + retryCount);
    if (retryCount < 3)
    {
    Con.Open();
    using (SqlDataAdapter dA = new SqlDataAdapter(cmd))
    {
    dA.Fill(dT);
    }
    Con.Close();
    }
    Trace.TraceInformation("after sqlExecute completes-retryCount-" + retryCount);
    }
    catch (SqlException ex)
    {
    Trace.TraceInformation("Error in sqlExecute-" + ex.Message);
    Thread.Sleep(TimeSpan.FromSeconds(1));
    Con.Close();
    retryCount++;
    goto retry;
    }
    return dT;
    }
    +1
  • Laxmidhar Sahoo
    +1
    hi Bikash
     
    Define a viewmodel like
     
    public class users
    {
    public string UserName {get;set;}
    public string Email {get;set;}
    }
    Data = repository.GetAlluser.select(c => new users() { Email = c.Email,UserName= c.UserName })
    var response = Request.CreateResponse(HttpStatusCode.Created, Data);
    +1
  • +1
    You can create the stored procedure to retreive the necessary fields in the database server and then you can use this stored procedure to return the necessary fileds in the API controller.  
     
    Another solution is Firstly create class with these fields.
    public string Username {set;get;}
    public string password {set;get;}
    and then
    public IEnumerable Get()
    {
    using (EmployeeDBEntities entities = new EmployeeDBEntities())
    {
    return entities.Employees.select(c => new users() { Username = c.username,password= c.password }).ToList();
    }
    }
    +1