I am building a web api in C#, and its my first time using C# to build a web api. There is nothing special happening; we make a call to a stored procedure, and return the results as JSON.
I need to limit access to authenticated users. I added [Authorize]
to the controller, which works to the extent that it redirects to the logon page, even when the user is already authenticated. [Authorize]
is not working as expected. There is an existing application, so I can not change any global settings. What should I do?
Code sample below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using XXXX.XXXX.Web.Areas.api.Models;
namespace XXXX.XXXX.Web.Areas.api.Controllers
{
[Authorize]
public class ReportController : Controller
{
//
// GET: /api/Report/
public ActionResult Index()
{
return View();
}
//
// GET: /api/Report/RiskRatingSnapshot
public JsonResult RiskRollForward(string type)
{
var GET = Request.QueryString;
if (type != "Details") type = "";
var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PaynetDatabase"].ConnectionString);
var command = new SqlCommand("procPrptAPDPortMgrRollForwardDetails", connection)
{
CommandType = CommandType.StoredProcedure
};
command.Parameters.AddWithValue("@subid", 0);
command.Parameters.AddWithValue("@portfolio", GET["portfolio"]);
command.Parameters.AddWithValue("@currentDateKey", GET["currentDateKey"]);
command.Parameters.AddWithValue("@priorDateKey", GET["priorDateKey"]);
command.Parameters.AddWithValue("@exposureFrom", GET["exposureFrom"]);
command.Parameters.AddWithValue("@exposureTo", GET["exposureTo"]);
command.Parameters.AddWithValue("@APDSensitivity", GET["APDSensitivity"]);
if (type == "Details")
{
command.Parameters.AddWithValue("@groupId", GET["groupId"]);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
var table = pack(reader);
connection.Close();
return Json(table, JsonRequestBehavior.AllowGet);
/*************************************************************
--Example:
DECLARE @return_value int
EXEC @return_value = [dbo].[procPrptAPDPortMgrRollForwardDetails]
@subid = 0,
@portfolio = N'52,53',
@currentDateKey = 20111001,
@priorDateKey = 20110701,
@APDSensitivity = 0.25,
@exposureFrom = 0,
@exposureTo = 1000000000,
@groupId = 2
GO
**************************************************************/
}
return null;
}
private List<DetailsReport> pack(SqlDataReader reader)
{
List<DetailsReport> table = new List<DetailsReport>();
while (reader.Read())
{
DetailsReport row = new DetailsReport();
row.customer_number = reader["customer_number"].ToString();
row.customer_name = reader["customer_name"].ToString();
row.portfolio = Convert.ToInt32( reader["portfolio"].ToString() );
row.portname = reader["portname"].ToString();
row.state = reader["state"].ToString();
row.exposure_cur = reader["exposure_cur"].ToString();
row.exposure_chg = reader["exposure_chg"].ToString();
row.number_of_lenders = Convert.ToInt32( reader["number_of_lenders"].ToString() );
row.member_lender_business_unit = reader["member_lender_business_unit"].ToString();
row.LastKnownDel = reader["LastKnownDel"].ToString();
row.CurDelStatus = reader["CurDelStatus"].ToString();
row.PayNet_absolutePD_4q = reader["PayNet_absolutePD_4q"].ToString();
row.APD_4QChg = reader["4QAPD_Chg"].ToString();
row.PD_chg_wtd_cur_exp = reader["PD_chg_wtd_cur_exp"].ToString();
row.expWtdPD_cur = reader["ExpWtdPD_cur"].ToString();
row.expWtdPD_chg = reader["expWtdPD_chg"].ToString();
table.Add(row);
}
return table;
}
}
}