Monday, 23 May 2016

How to more than one list value to add another list value in MVC?

 *************Employee Model*******************
 public classEmployeeModel
    {
        public EmployeeModel()
        {
            EmployeeList = new List<EmployeeModel>();
            StudentList = new List<EmployeeModel>();

            ServiceList = new List<SelectListItem>();
            ParentList = new List<SelectListItem>();
            GenderList= new List<SelectListItem>
            {
               new SelectListItem() {Text = "--Select--", Value ="0" },
               new SelectListItem() {Text = "Male", Value ="1" },
               new SelectListItem() {Text = "Female", Value ="2"},
             };
        }
        public int ID { get; set; }
        public string Name{ get; set; }
        public string Address{ get; set; }
        public int Age{ get; set; }
        public string Mobile{ get; set; }
     
        public List<EmployeeModel> EmployeeList { get; set; }
        public List<EmployeeModel> StudentList { get; set; }
        public IEnumerable<Employee> SerList { get; set; }

        public decimal? ServiceUsage { get; set; }
        public decimal? ServiceCost_Per_Annum { get; set; }
        public decimal? ServiceCost_for_Service { get; set; }

        public List<SelectListItem> GenderList{ get; set; }

    }

************************End Employee Model**************************
var empList = _apiService.GetAllEmployee().ToList();
                    model.EmployeeList = stcList.Select(x =>
                    {
                        return new EmployeeModel()
                        {
                            ID = x.ID == null ? 0 : x.ID.Value,
                            Name = string.IsNullOrEmpty(x.Name) ? "" : x.Name,
                            Address= string.IsNullOrEmpty(x.Address) ? "" : x.Address,
                            Mobile= string.IsNullOrEmpty(x.Mobile) ? "" : x.Mobile,
                            Age= x.Age == null ? Convert.ToInt32(0) : x.Age,
                        };
                    }).ToList();

                    var stuList = _apiService.GetAllStudent().ToList();
                    model.StudentList = stuList .Select(x =>
                        {
                            return new ComponentModel()
                            {
                               ID = x.ID == null ? 0 : x.ID.Value,
                            Name = string.IsNullOrEmpty(x.Name) ? "" : x.Name,
                            Address= string.IsNullOrEmpty(x.Address) ? "" : x.Address,
                            Mobile= string.IsNullOrEmpty(x.Mobile) ? "" : x.Mobile,
                            Age= x.Age == null ? Convert.ToInt32(0) : x.Age,
                            };
                        }).ToList();


var AddExData = new List<EmployeeModel>();

AddExData.AddRange(model.EmployeeList);
AddExData.AddRange(model.StudentList);

Sunday, 1 May 2016

How to windows authentication in mvc?

Firstly, in web.config to configure-:

<system.web>  
    <authentication mode="Windows"></authentication>
    <authorization>
      <deny users="?" />
    </authorization>
 </system.web>

After that Project property (F4 Click)
Windows Authentication Diable and Autonymous Authentication Enabled.

After that it works.
  string domainUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            string IsAuthentication = System.Web.HttpContext.Current.Request.IsAuthenticated.ToString();
            string[] paramsLogin = domainUser.Split('\\');
            string UserName = paramsLogin[1].ToString();

How to log out windows authentication in mvc?

public ActionResult LogOut()
{
    HttpCookie cookie = Request.Cookies["TSWA-Last-User"];

    if(User.Identity.IsAuthenticated == false || cookie == null || StringComparer.OrdinalIgnoreCase.Equals(User.Identity.Name, cookie.Value))
    {
        string name = string.Empty;

        if(Request.IsAuthenticated)
        {
            name = User.Identity.Name;
        }

        cookie = new HttpCookie("TSWA-Last-User", name);
        Response.Cookies.Set(cookie);

        Response.AppendHeader("Connection", "close");
        Response.StatusCode = 401; // Unauthorized;
        Response.Clear();
        //should probably do a redirect here to the unauthorized/failed login page
        //if you know how to do this, please tap it on the comments below
        Response.Write("Unauthorized. Reload the page to try again...");
        Response.End();

        return RedirectToAction("Index");
    }

    cookie = new HttpCookie("TSWA-Last-User", string.Empty)
    {
        Expires = DateTime.Now.AddYears(-5)
    };

    Response.Cookies.Set(cookie);

    return RedirectToAction("Index");

}