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();
            xdoc.Load(webConfigPath);
            var endpointNodes = xdoc.SelectSingleNode("/configuration/system.serviceModel/client /endpoint");
            if (endpointNodes != null)
                endpointNodes.Attributes["address"].Value = addressValue;

            xdoc.Save(webConfigPath); 

 

Search

Custom Search
Protected by Copyscape Plagiarism Software