locked
Convert MVC Action to WebAPI RRS feed

  • Question

  • User490317677 posted

    Hi :)

    I'm new in ASP.Net WebAPI and i have Login Action in MVC5 ,where i want to get work also in WebAPI and of course with some modifying, but to be honest i dont know how to really do it , i tried some different things but still no luck :(

    This is what i have in my ActionResult, Which is Work Fine in MVC5 but i dont know how to write something like this in WebAPI and get it work in WebAPI , can anyone please point me in right direction! Thx

            public ActionResult Login(string Mail, string CustomerNo, string pass)
            {
    
    
                var hashedPass = PasswordSecurity.PasswordStorage.CreateHash(pass);
                using (DataContext db = new DataContext())
                {
                 
                    var query = from cbr in db.Contact_Business_Relation
                                join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
    
                                join sa in db.Sales_Header on cbr.No_ equals sa.Sell_to_Customer_No_
                                join px in db.PX2 on c.E_Mail equals px.Email_ID
    
                                where c.E_Mail == Mail.ToLower()
                                select new
                                {
                                    Mail = c.E_Mail, 
                                    pass = px.PS, 
                                    CustomerNo = sa.Sell_to_Customer_No_ 
                                };
    
                    var user = query.FirstOrDefault();
                    var CheckPass = PasswordSecurity.PasswordStorage.VerifyPassword(pass, user.pass);
    
                    if (user != null && CheckPass)
                    {
                        Session["CustomerNo"] = user.CustomerNo.ToString();
                        Session["Email"] = user.Mail.ToString();
                        FormsAuthentication.SetAuthCookie(Mail, false);
    
                        return RedirectToAction("Index");
                    }
    
    
                    else
                    {
                        ModelState.AddModelError("", "Adgangskode er ikke gyldig / Password is not valid");
    
                    }
    
                }
                return View();
    
            }

    and this what i have in my API Controller :

            [Route("api/edapi/Login/{Mail}/{pass}")]
            [HttpPost]
            public object Login(string Mail, string CustomerNo, string pass) 
            {
    
                var hashedPass = PasswordSecurity.PasswordStorage.CreateHash(pass);
                using (DataContext db = new DataContext())
                {
                  
    
    
                    var query = from cbr in db.Contact_Business_Relation
                                join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
    
                                join sa in db.Sales_Header on cbr.No_ equals sa.Sell_to_Customer_No_
                                join px in db.PX2 on c.E_Mail equals px.Email_ID
    
                                where c.E_Mail == Mail.ToLower()
                                select new 
                                {
                                    Mail = c.E_Mail,
                                    pass = px.PS,
                                    CustomerNo = sa.Sell_to_Customer_No_
                                };
    
                    var user = query.FirstOrDefault();
                    var CheckPass = PasswordSecurity.PasswordStorage.VerifyPassword(pass, user.pass);
    
                    return user; 
    
                }

    Monday, April 9, 2018 9:31 AM

Answers

  • User-369506445 posted

    hi

    you can try below code :

    public IHttpActionResult Login(string Mail, string CustomerNo, string pass)
            {
               var hashedPass = PasswordSecurity.PasswordStorage.CreateHash(pass);
                using (DataContext db = new DataContext())
                {
                  
    
    
                    var query = from cbr in db.Contact_Business_Relation
                                join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
    
                                join sa in db.Sales_Header on cbr.No_ equals sa.Sell_to_Customer_No_
                                join px in db.PX2 on c.E_Mail equals px.Email_ID
    
                                where c.E_Mail == Mail.ToLower()
                                select new 
                                {
                                    Mail = c.E_Mail,
                                    pass = px.PS,
                                    CustomerNo = sa.Sell_to_Customer_No_
                                };
    
                    var user = query.FirstOrDefault();
                    
    
                return Ok(user);
            }

    the action api return a json class 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 9, 2018 11:26 AM

All replies

  • User-369506445 posted

    hi

    you can try below code :

    public IHttpActionResult Login(string Mail, string CustomerNo, string pass)
            {
               var hashedPass = PasswordSecurity.PasswordStorage.CreateHash(pass);
                using (DataContext db = new DataContext())
                {
                  
    
    
                    var query = from cbr in db.Contact_Business_Relation
                                join c in db.Contact on cbr.Contact_No_ equals c.Company_No_
    
                                join sa in db.Sales_Header on cbr.No_ equals sa.Sell_to_Customer_No_
                                join px in db.PX2 on c.E_Mail equals px.Email_ID
    
                                where c.E_Mail == Mail.ToLower()
                                select new 
                                {
                                    Mail = c.E_Mail,
                                    pass = px.PS,
                                    CustomerNo = sa.Sell_to_Customer_No_
                                };
    
                    var user = query.FirstOrDefault();
                    
    
                return Ok(user);
            }

    the action api return a json class 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 9, 2018 11:26 AM
  • Monday, April 9, 2018 11:29 AM
  • User-474980206 posted

    when you use a login api you have a couple decisions

    1) where to store the login ticket. the action uses a cookie. the api has a choice of cookie, header token, or parameter token.

    2) the login need to return success or failure

    3) as the token can expire, every api call needs a way to return login required (redirect to login page will not work)

    Monday, April 9, 2018 2:23 PM
  • User490317677 posted

    Thx Bruce,it was useful :)

    Sunday, April 15, 2018 4:42 PM
  • User490317677 posted

    Thannks Vahid :)

    Sunday, April 15, 2018 4:42 PM