Wednesday, January 11, 2017

OnAuthorization in MVC

 public void OnAuthorization(AuthorizationContext filterContext)
            var skipAutherization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||                                filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);
            if (!skipAutherization)
              if (SessionManagement.UserID == Guid.Empty)
                   filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { Controller = "Account", Action = "Login" }));

Using above code we can restrict/authenticate the particular actions and controllers 

How to get IPAddress,Browser name and Browser Version

 var ipAddress = Request.ServerVariables["remote_addr"];  //for getting IPAddress of the User 
var userAgent = string.Format("Browser ({0}) : {1}", Request.Browser.Browser, Request.Headers["User-Agent"]);  ///will get browser name and browser version

 var hostName= Dns.GetHostEntry("ip").HostName; // will get computer name of the user 

Update Xml Element Using C#

           var xdoc = new XmlDocument();
            var endpointNodes = xdoc.SelectSingleNode("/configuration/system.serviceModel/client /endpoint");
            if (endpointNodes != null)
                endpointNodes.Attributes["address"].Value = addressValue;



Read and write Xml using C#

          var xdoc = new XmlDocument();
            var connStringnodes = xdoc.SelectSingleNode("/configuration/connectionStrings");

            if (connStringnodes != null)
                if (connStringnodes.ChildNodes.Count > 0)
                    var elementNodeList = xdoc.GetElementsByTagName("connectionStrings");
                    foreach (XmlNode node in elementNodeList)
                        while (node.FirstChild != null)
                var addElement = xdoc.CreateElement("add");
                addElement.SetAttribute("name", "conStringname");
                addElement.SetAttribute("connectionString", connectionStringValue);
                addElement.SetAttribute("providerName", providerNameValue);


Above code will Change the Connectionstring child elements... 


string somestring = "One,,Two,,,Three,,,Four,,";
somestring.Split(new char[] { ',' },   StringSplitOptions.RemoveEmptyEntries)

StringSplitOptions.RemoveEmptyEntries- Can remove empty strings after comma seperated from the above code

500.19 - Internal Server Error

 The requested page cannot be accessed because the related  configuration data for the page is invalid.

500.19 - Internal Server Error - The requested page cannot be accessed because the related configuration data for the page is invalid..

if we get error like above we can add below lines in web.config file 
 Unsubscribe.aspx--> accessing page in my scenario.
<location path="Unsubscribe.aspx">
        <allow users="*"/>

NetBIOS name using C#

 Gets the NetBIOS name of this local computer. 

Using Unit of Work in Controller

UnitOfWork unitOfWork = new UnitOfWork();
var profile = unitOfWork.ProfileRepository.Get(filter: p => p.Code == login);

in the above code filter: will work as where condition 

Model Error in MVC

ModelState.AddModelError("", "Invalid username or password."); 

How to Remove Plural table name from DbContext

 protected override void OnModelCreating(DbModelBuilder modelBuilder)


by adding this line we can remove the plural table name from DbContext


Custom Search
Protected by Copyscape Plagiarism Software