Monday, January 25, 2016

RDLC report error on MVC/Webform hybrid website: The Value expression for the query parameter contains an error : Request for the permission of type System.Security.Permissions.SecurityPermission failed.

When creating RDLC report on MVC/Webform hybrid website, the following error can occur.

The Value expression for the query parameter contains an error : Request for the permission of type System.Security.Permissions.SecurityPermission failed.

Quick workaround to address this issue is by setting up permission on local report like below.

// Default.aspx.cs
...
using System.Security;
using System.Security.Permissions;

namespace MyProj.Web.Reports
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if(!Page.IsPostBack)
    {
      ...
      ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/RDLC/Report1.rdlc");
      ReportViewer1.LocalReport.SetBasePermissionsForSandboxAppDomain(
        new PermissionSet(PermissionState.Unrestricted));
      ReportViewer1.LocalReport.Refresh();
    }
  }
}

Friday, January 22, 2016

Remove Password Change Requirement in AD

For test AD environment, it is often convenient to disable the default password requirements such as minimum length, expiration days, etc. Here's a quick cheat sheet on how to do it.


  1. Remote in to Domain Controller
  2. Run "Group Policy Management"
    1. Expand Forest, Domains and your AD domain
    2. Right-click the "Default Domain Policy" and select "Edit"
    3. Group Policy Management Editor appears.
      1. Expand Computer Configuration
      2. Expand Policies
      3. Expand Windows Settings
      4. Expand Security Settings
      5. Expand Account Policies
      6. Select Password Policy
        1. Enable policies with 0 values as needed in the right pane.
      7. Select Account Lockout Policy
        1. Make changes as needed.
  3. On Windows 2012 or later
    1. Use Powershell command "Invoke-GPUpdate"
  4. On Windows 2008 or earlier
    1. Use command "gpupdate /force"

Tuesday, December 29, 2015

WEBCONFIG.ACCSRV.XML does not have element "configuration/configSections/sectionGroup[@name='SharePoint']" or it is invalid

I recently updated SharePoint 2013 environment using the December 2015 CU packages and ran into an error. There are two (2) SharePoint 2013 servers in the environment. One Web-Front-End SharePoint Server and one Service Application SharePoint Server. On the Service Application Server, the following error message appeared during step 8 of SharePoint Products Configuration Wizard, which I ran after installing all December 2015 CU successfully.

Configuration Failed
...
Failed to install the application content files.
An exception of type System.IO.InvalidDataException was thrown. Additional exception information: The web configuration file, C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\config\WEBCONFIG.ACCSRV.XML, does not have element "configuration/configSections/sectionGroup[@name='SharePoint']" or it is invalid.

First I stopped Access Database Service 2010 and Access Services from the Application Service Server and then ran the Config Wizard again. It did not make a difference.

Then I stopped all services except "SharePoint Server Search" on the Application Service Server and ran the Config Wizard. It completed without error only then.

The following are the list of services I stopped on the Application Service Server.
  • Access Database Service 2010
  • Access Services
  • App Management Service
  • Business Data Connectivity Service
  • Central Administration
  • Claims to Windows Token Service
  • Distributed Cache
  • Document Conversions Launcher Service
  • Document Conversions Load Balancer Service
  • Excel Cacluation Services
  • Lotus Notes Connector (was never turned on)
  • Machine Translation Service (was never turned on)
  • Managed Metadata Web Service
  • Microsoft SharePoint Foundation Incoming E-Mail
  • Microsoft SharePoint Foundation Sandboxed Code Service
  • Microsoft SharePoint Foundation Subscription Settings Service
  • Microsoft SharePoint Foundation Web Application
  • Microsoft SharePoint Foundation Workflow Timer Service
  • PerformancePoint Service
  • PowerPoint Conversion Service
  • Search Host Controller Service
  • Search Query and Site Settings Service
  • Secure Store Service
  • User Profile Service
  • User Profile Synchronization Service
  • Visio Graphics Service
  • Word Automation Services
  • Work Management Service

Also, the lesson I learned was that in SharePoint 2013, it would be best to turn on only the absolutely necessary services or service applications and turn off the ones if they are not absolutely necessary for the sake of the best predictable results in patch installation. 

Thursday, October 22, 2015

One-to-one foreign key relationship in Entity Framework

This site seems to have useful contents for those who want to re-visit their knowledge about Entity Framework.

The example on "One-to-One or One-to-Zero Foreign Key Relationship" was especially useful.

Take a look.

Site Url = http://www.EntityFrameworkTutorial.net


Friday, October 16, 2015

Genenrate full Url (absolute Url) using C# in ASP.NET MVC

A quick way to generate full Url (absolute Url) in ASP.NET MVC:

Url.Action("Details", "Product", new { id = p.ProductID}, Request.Url.Scheme)


If without id, simply use null in the parameter.

Url.Action("Details", "Product", null, Request.Url.Scheme)

Wednesday, October 7, 2015

Stop screensaver autolock

A quick VBScript could be useful in keeping computer active without screensaver automatically taking over. For security policies, most companies have automatic screensaver or session lock in place based on user inactivity. For those of us who need to keep the system monitor PC on all the time, this script could be useful. It basically sends escape key stroke once a minute for the duration of time you specify.

Dim shell, minutes, minuteCount
minutes = 300

Set shell = CreateObject("WScript.Shell")
minuteCount = 0
For minuteCount = 0 To minutes
  shell.SendKeys "{ESC}"
  minuteCount = minuteCount + 1
  WScript.Sleep(60000) 
Next

Just don't forget to lock the computer when you step away from the computer.
To stop the script, close "WScript.exe" in Task Manager or logout from Windows.

Sunday, October 4, 2015

Custom Error Page in ASP.NET MVC

Setting up custom error page for 400 (bad request), 401 (unauthorized), 403 (forbidden), 404 (not found), 500 (internal server error) seemed like a web.config task as simple as specifying
    
<system.web>
    <customErrors mode="On" redirectMode="ResponseRedirect"  >
      <error statusCode="400" redirect="~/CustomError/BadRequest"/>
      <error statusCode="401" redirect="~/CustomError/Unauthorized" />
      <error statusCode="404" redirect="~/CustomError/NotFound"  />
      <error statusCode="403" redirect="~/CustomError/Forbidden"/>
      <error statusCode="500" redirect="~/CustomError/InternalServerError"/>
    </customErrors>
</system.web>
However, the above may not allow our custom error pages to handle certain cases.

For example, the default IIS 404 error page, not my custom error page, will handle 404 for url that is not understood by my MVC application's routing rule (i.e., http://localhost/MyApp/a/b/c/d/e/f).

It may be easier to use <httpErrors> under <system.webServer> rather than <customErrors> in <system .web>. The above can be converted to below. Note that we cannot use ~/ in the path attribute. <system.webServer> is IIS-level override for customError handling in this case. So remove the above and try something like the following.

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace">      
    <remove statusCode="400" />
    <remove statusCode="401" />
    <remove statusCode="403" />
    <remove statusCode="404" />
    <remove statusCode="500" />
    <error statusCode="400" path="/MyApp/CustomError/BadRequest" responseMode="ExecuteURL"/>
    <error statusCode="401" path="/MyApp/CustomError/Unauthorized" responseMode="ExecuteURL"/>
    <error statusCode="403" path="/MyApp/CustomError/Forbidden" responseMode="ExecuteURL"/> 
    <error statusCode="404" path="/MyApp/CustomError/NotFound" responseMode="ExecuteURL"/>
    <error statusCode="500" path="/MyApp/CustomError/InternalServerError" responseMode="ExecuteURL"/>
  </httpErrors>
</system.webServer>


Below is the Controller


public class CustomErrorController : Controller
{
 public ActionResult BadRequest()
 {
  Response.StatusCode = 400;
  ViewBag.ErrorCode = "400";
  ViewBag.ErrorTitle = "400 Error - Bad Request";
  ViewBag.ErrorMessage = 
   "The resource you tried to access appears to be incomplete.\n" + 
   "Please check your URL and try again.";
  return View("Error");
 }
 public ActionResult Unauthorized()
 {
  Response.StatusCode = 401;
  ViewBag.ErrorCode = "401";
  ViewBag.ErrorTitle = "401 Error - Unauthorized";
  ViewBag.ErrorMessage = 
   "The resource you tried to access appears to require a user permission.\n" + 
   "Please re-launch the browser and try again.";
  return View("Error");
 }
 public ActionResult Forbidden()
 {
  Response.StatusCode = 403;
  ViewBag.ErrorCode = "403";
  ViewBag.ErrorTitle = "403 Error - Forbidden: Access is denied";
  ViewBag.ErrorMessage = 
   "The resource you tried to access is not allowed. \n" + 
   "Please check your URL and try again.";
  return View("Error");
 }
 public ActionResult NotFound()
 {
  Response.StatusCode = 404;
  ViewBag.ErrorCode = "404";
  ViewBag.ErrorTitle = "404 Error - Resource Not Found";
  ViewBag.ErrorMessage = 
   "The resource you tried to access was not found.\n" + 
   "Please check your URL and try again.";
  return View("Error");
 }
 public ActionResult InternalServerError()
 {
  Response.StatusCode = 500;
  ViewBag.ErrorCode = "500";
  ViewBag.ErrorTitle = "500 Error - Internal Server Error";
  ViewBag.ErrorMessage = 
   "An error occurred while system tried to process your request.\n" + 
   "Try again, and if problem persists contact system administrator.";
  return View("Error");
 }
}


And the View

@{
    ViewBag.Title = ViewBag.Title;
}

<div style="text-align: center;">
    <div style="font-size: 10em; color:#ccc; font-weight: bold; ">
        @ViewBag.ErrorCode
    </div>

    <h2>
        @ViewBag.ErrorTitle
    </h2>

    <h3 style="white-space: pre-wrap; word-wrap: break-word;">@ViewBag.ErrorMessage</h3>

    <h4>
        <a href="javascript:void(0);" onclick="javascript: history.go(-1);">Back</a>
    </h4>
</div>


And this is the custom 404 page.