Most frequently asked oops interview questions

Posted by Unknown 0 comments
Most frequently asked oops interview questions

                   In this post am going tell you about most frequently asked oops interview questions,In previous post i have explained  Most frequently asked interview questions in mvc Please read below about Oops questions


  1. What is object-oriented programming (OOP)?
  2. What is class? Give a real life example
  3. What is an object? Give a real life example.
  4. Explain the basic features of OOPs
  5. What is the relationship between a class and an object?
  6. What is difference between OOP and procedural Language?
  7. What is encapsulation? Why encapsulation is necessary, explain with example?
  8. Tell about access modifier
  9. What is value type and reference type, explain with example?
  10. What is polymorphism, explain with example?
  11. What is run time polymorphism, explain with example?
  12. How is method overriding different from method overloading?
  13. What is inheritance, explain with example?
  14. What is interface? Why interface is used?
  15. What is abstract class? Why abstract class is used?
  16. What is static member? Why static member? How static member is accessed?
  17. Can you specify the accessibility modifier for methods inside the interface?
  18. What is constructor? Why constructor is used?
  19. What is constructor overloading?
  20. Is it possible for a class to inherit the constructor of its base class?
  21. What is the difference between arrays and collection?
  22. What are collections and generics?
  23. How can you prevent your class to be inherited further?
  24. What is the index value of the first element in an array?


Hope this will helpful
Labels:

Most frequently asked interview questions in mvc

Posted by Unknown 0 comments

    In post i am going to write about most frequently asked interview questions in asp.net mvc.
Related Post:Most frequently asked oops interview questions

  • Explain MVC (Model-View-Controller) in general?
  • What is ASP.NET MVC?
  • Difference between ASP.NET MVC and ASP.NET WebForms?
  • What are the Core features of ASP.NET MVC?
  • Can you please explain the request flow in ASP.NET MVC framework?
  • What is Routing in ASP.NET MVC?
  • What is the difference between ViewData, ViewBag and TempData?
  •  What are Action Methods in ASP.NET MVC?
  • Explain the role of Model in ASP.NET MVC?
  • What are Action Filters in ASP.NET MVC?
Please go through the answers below

1. Explain MVC (Model-View-Controller) in general?

MVC (Model-View-Controller) is an architectural software pattern that basically decouples various components of a web application. By using MVC pattern, we can develop applications that are more flexible to changes without affecting the other components of our application.

  •  "Model", is basically domain data.
  •  "View", is user interface to render domain data.
  •  "Controller", translates user actions into appropriate operations performed on model.


2. What is ASP.NET MVC?

ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework.


3. Difference between ASP.NET MVC and ASP.NET WebForms?

ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front controller approach. In case of Page controller approach, every page has its own controller i.e. code-behind file that processes the request. On the other hand, in ASP.NET MVC, a common controller for all pages processes the requests.



4. What are the Core features of ASP.NET MVC?

Core features of ASP.NET MVC framework are:
  • Clear separation of application concerns (Presentation and Business Logic) 
  • An extensible and pluggable framework
  • Extensive support for ASP.NET Routing
  • Support for existing ASP.NET features


5. Can you please explain the request flow in ASP.NET MVC framework?

Request flow for ASP.NET MVC framework is as follows:

Request hits the controller coming from client. Controller plays its role and decides which model to use in order to serve the request. Further passing that model to view which then transforms the model and generate an appropriate response that is rendered to client.


6. What is Routing in ASP.NET MVC?

In case of a typical ASP.NET application, incoming requests are mapped to physical files such as .aspx file. ASP.NET MVC framework uses friendly URLs that more easily describe user’s action but not mapped to physical files.

ASP.NET MVC framework uses a routing engine, that maps URLs to controller classes. We can define routing rules for the engine, so that it can map incoming request URLs to appropriate controller. 

Practically, when a user types a URL in a browser window for an ASP.NET MVC application and presses “go” button, routing engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller.



7. What is the difference between ViewData, ViewBag and TempData?

In order to pass data from controller to view and in next subsequent request, ASP.NET MVC framework provides different options i.e. ViewData, ViewBag and TempData.

Both ViewBag and ViewData are used to to communicate between controller and corresponding view.But this communication is only for server call, it becomes null if redirect occurs. So, in short, its a mechanism to maintain state between controller and corresponding view.

ViewData is a dictionary object while ViewBag is a dynamic property (a new C# 4.0 feature). viewData being a dictionary object is accessible using strings as keys and also requires typecasting for complex types.On the other hand, ViewBag doesn't have typecasting and null checks.

TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects i.e from one controller to the other controller.


8. What are Action Methods in ASP.NET MVC?

As I already explained about request flow in ASP.NET MVC framework that request coming from client hits controller first. Actually MVC application determines the corresponding controller by using routing rules defined in Global.asax. And controllers have specific methods for each user actions. Each request coming to controller is for a specific Action Method. The following code example, “ShowBooks” is an example of an Action Method.

public ViewResult ShowBooks(int id)
{
  var computerBook = db.Books.Where(p => P.BookID == id).First(); 
  return View(computerBook);
}


9.Explain the role of Model in ASP.NET MVC?

One of the core feature of ASP.NET MVC is that it separates the input and UI logic from business logic. Role of Model in ASP.NET MVC is to contain all application logic including validation, business and data access logic except view i.e. input and controller i.e UI logic.

Model is normally responsible for accessing data from some persistent medium like database and manipulate it.


10.What are Action Filters in ASP.NET MVC?

If we need to apply some specific logic before or after action methods, we use action filters. We can apply these action filters to a controller or a specific controller action. Action filters are basically custom classes that provide a mean for adding pre-action or post-action behavior to controller actions.
For example,
  • Authorize filter can be used to restrict access to a specific user or a role.
  • OutputCache filter can cache the output of a controller action for a specific duration.


Labels: ,

How to call restful web service using http webrequest

Posted by Unknown 0 comments
Call Restful web service using Httpwebrequest

In This article am going explain about how to call the restful web service using the Httpwebrequest

WCF Restful service 


Contract
[ServiceContract]
public interface IRestService
{
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/MyServiceMethod")]
[OperationContract]
void MyServiceMethod(PostedInformation postedInformations);
}
Implementation
public void MyServiceMethod(PostedInformation postedInformations)
{
//do implemention code here
}
DataContract
[DataContract]
public class PostedInformation
{
[DataMember]
public List<string> To { get; set; }
[DataMember]
public string SenderEmail { get; set; }
[DataMember]
public string Subject { get; set; }
[DataMember]
public string SenderFullName { get; set; }
}
Calling Rest service using HTTPWebRequest

var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject("you object to be serialize" ));
//Passyour service url to the create method
var req =
HttpWebRequest.Create(“http://localhost/MyServer/YourServiceName.svc/MyServiceMethod);
req.ContentType = “application/json”;
req.ContentLength = dataToSend.Length;
req.Method = “POST”;
req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);
var response = req.GetResponse();

I hope this will helps
Labels:

Building sample web api application in asp net mvc 4

Posted by Unknown 0 comments
  
Building sample web APIapplication in asp net mvc 4

         In This post am going explain you building simple web API application in asp .net MVC 4,here i have given step by step explanation about web API implementation  

Open Visual Studio 2012 >> File >> New >> Project >> Select ASP.NET MVC4 Web Application Template >> Select Web API Template
1
2

















It will add automatically OData dependencies for us.Also you can install OData dependencies by using Nuget as well.
3
Create a new controller namedCustomersController by rightclicking the Controller folder.
9




Then add new class namedCustomer in the Model folder and add some properties like below

4
Also add one more folder named Domain and add a class namedCustomerDataContext, It contains a method Customers which return some customers information.
5

    For setting the OData first i need to do is set up my routing that can be get in to the Customers controller based on OData routing scheme not traditional web api routing schemes.Go to the WebApiConfig class in the App_Start folder and add a helper method GetImplicitEdm for defining entity data model.

6
                             This method will return IEdmModel which is a interface abstraction on the top of entity data models.ODataModelBuilder class will create set most of the entity data models automatically for you.The main thing you tell to the builder is entity sets are involved and then call the GetEdmModel method associated with the builder class,this will find all the properties on the object,find the types of those properties,look for navigation properties relates other objects in the model. Also add one more route (marked as yellow above) other than the web api route by calling the extension method MapODataRoute which accepts the parameters such as name of the route,a route prefix similar to the api route prefix in the default web api route and then call our helper method GetImplicitEdm.

                                  Then the next step is go to the customers controller and change our base classApiController to EntitySetController which is generic type takes two arguments,first one is the type of the entity controller represents and second one is the type of key property in that customer object and override Get method of type IQueryble and return our Customers.
8







Run the application and browse the url like below.It will return all the customers.
10







Then i am going to take individual customers. To do that there is a separate override base class called GetEntityByKey and querying by using linq.Note that this is a protected method it expose the service operation is actually a different public method on our base class and the public method will call this one.
11



Go and run the application and browse the url like below.
localhost:61592/odataCustomers("Niju")

I hope this will helps 


Labels:

How to Add meta tag using java Script dynamically

Posted by Unknown 0 comments

How to Add meta tag using java Script dynamically
      Today i found following script which can be useful for adding meta tags dynamically through the java script,based on code snippet given below we can meta tags dynamically based on device.

<script type="text/javascript">
$(document).ready(function () {
$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
var isMobile = {
Android: function () {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function () {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function () {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function () {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function () {
return navigator.userAgent.match(/IEMobile/i);
},
any: function () {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if (isMobile.Android() && $.browser.chrome) {
document.head.insertAdjacentHTML('beforeEnd', '<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=0" />');
document.getElementsByTagName('head').innerText = document.head.innerHTML;
}
else if (isMobile.Android()) {
document.head.insertAdjacentHTML('beforeEnd', '<meta name="viewport" content="width=device-width, initial-scale=0.0, minimum-scale=0.0, maximum-scale=0.0,user-scalable=0" />');
document.getElementsByTagName('head').innerText = document.head.innerHTML;
} else {
document.head.insertAdjacentHTML('beforeEnd', '<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=0" />');
document.getElementsByTagName('head').innerText = document.head.innerHTML;
}
});
</script>
Labels:

Custom password validation rules in asp.net identity

Posted by Unknown 0 comments

Custom password validation rules  in asp.net identity

                To give more secure experience to  application, we have use complex password security rules like it should be the minimum password length,should have 1 capital letter and special characters etc.

This post will give the few more additional password rules implementation in asp.net Please read the below 

We will create an application using Visual Studio 2013, update the Identity assemblies to 2.0.0-alpha1, and then add code to enforce the following password policies:
i. Change the default password length requirement from 6 characters to 10 characters.
ii. The password should have at least one number and one special character.
iii. The user cannot reuse any of the last 5 previous passwords.
Lastly we’ll run the application to verify if these policies have been successfully enforced.

Create application

  • In Visual Studio 2013 RTM create a new 4.5 ASP.NET Web application. Choose MVC.
image
  • Right-click the project and select ‘Manage Nuget Packages’. Go to the ‘Update’ section and update the Identity packages to 2.0.0-alpha1
image
  • Let’s quickly look at the user creation flow in the application. This will help us outline the password validation steps in the Identity system. Under the Controllers folder, the AccountController has actions for user account management.
    • The Account controller uses an instance of UserManager class which is defined in the Identity framework. This takes a UserStore class which has persistence-specific API for user management. In our case it uses Entity Framework.
    • The Register post method creates a new user. The call to CreateAsync in turn calls the ‘ValidateAsync’ method on the PasswordValidator property to validate the supplied password.
 1: var user = new ApplicationUser() { UserName = model.UserName };
 2:  
 3: var result = await UserManager.CreateAsync(user, model.Password);
 4:  
 5: if (result.Succeeded)
 6: {
 7: await SignInAsync(user, isPersistent: false);
 8:  
 9: return RedirectToAction("Index", "Home");
 10: }
 11:  
 
      
    • On success the user is signed in.
Similarly, password validation is called when the user tries to change the password. This can be observed in the ‘Manage’ post action.

Policy 1: Change the default password length requirement from 6 characters to 10 characters.

By default the PasswordValidator property in UserManager is set to the MinimumLengthValidator class which validates that the password length is at least 6. This value can be changed when instantiating a UserManager object.
  • The minimum password length value can be changed in the AccountController in the constructor where the UserManager is instantiated or at a global level by defining a separate class derived from UserManager. The second approach is possible by extending the UserManager class in the application and setting the property. Under the IdentityExtensions folder, create a new class ApplicationUserManager which extends from UserManager<ApplicationUser>.
    
 1: public class ApplicationUserManager : UserManager<ApplicationUser>
 2: {
 3:         public ApplicationUserManager(): base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
 4:         {
 5:               PasswordValidator = new MinimumLengthValidator (10);
 6:         }
 7: }
 8: 
The UserManager class takes an IUserStore class which is a persistence-specific implementation of user management APIs. Since the UserStore class is EntityFramework-specific, we need to pass in the DbContext object. This call is essentially the same as defined in the constructor of the AccountController class.
The additional advantage of defining the custom ApplicationUserManager class is that we can override methods defined in the UserManager class, which we will be doing for the subsequent password policies.
  • We then need to hook in the ApplicationUserManager class defined in the above step in the AccountController. Change the constructor and the UserManager Property in the AccountController as below.
      
 1: public AccountController() : this(new ApplicationUserManager())
 2:         {
 3:         }
 4:  
 5:         public AccountController(ApplicationUserManager userManager)
 6:         {
 7:             UserManager = userManager;
 8:         }
 9:         public ApplicationUserManager UserManager { get; private set; }

This way we are making the AccountController use the custom UserManager. This enables you to do additional customization while keeping the other code in the controller intact.
  • Run the application and try to register a local user. If the password is less than 10 characters in length, you’ll get the following error message.
image

Policy 2: Passwords should have at least one special character and one numeral

As mentioned in the previous section, the PasswordValidator property in the UserManager class validates the length of the password to 6 characters. Though we can change the ‘RequiredLength’, to add additional checks we would need to extend the behavior through a new class. We can add new password validators by creating a class that implements the IIdentityValidator interface and adding the validations as needed. In our case we want to check for special characters and numbers in the supplied password along with checking if it is of minimum length in the ‘ValidateAsync’ method.
  • Create a new folder under the root project called IdentityExtensions. Add a new class CustomPasswordValidator implementing IIdentityValidator<string> since password is of type string.
 1: public class CustomPasswordValidator : IIdentityValidator<string>
 2:  
 3: {
 4:  
 5: public int RequiredLength { get; set; }
 6:  
 7: public CustomPasswordValidator(int length)
 8:  
 9: {
 10:  
 11: RequiredLength = length;
 12:  
 13: }
 14:  
 15: public Task<IdentityResult> ValidateAsync(string item)
 16:  
 17: {
 18:  
 19: if (String.IsNullOrEmpty(item) || item.Length < RequiredLength)
 20:  
 21: {
 22:  
 23: return Task.FromResult(IdentityResult.Failed(String.Format("Password should be of length {0}",RequiredLength)));
 24:  
 25: }
 26:  
 27: string pattern = @"^(?=.*[0-9])(?=.*[!@#$%^&*])[0-9a-zA-Z!@#$%^&*0-9]{10,}$";
 28:  
 29: if (!Regex.IsMatch(item, pattern))
 30:  
 31: {
 32:  
 33: return Task.FromResult(IdentityResult.Failed("Password should have one numeral and one special character"));
 34:  
 35: }
 36:  
 37: return Task.FromResult(IdentityResult.Success);
 38:  
 39: }
In the ValidateAsync method, we check if the password is of the specified length. Then we use a regular expression to verify if the password has a special character and a numeric character. Note: The regular expression is not tested fully and should be used as a sample only.
  • Next we need to set this validator in the PasswordValidator property of the UserManager. Navigate to the ApplicationUserManager created earlier and change the PasswordValidator property from MinimumLengthValidator to CustomPasswordValidator.
 1: public class ApplicationUserManager : UserManager<ApplicationUser[PR6] >
 2:  
 3: {
 4:  
 5: public ApplicationUserManager()
 6:  
 7: : base(new UserStore<ApplicationUser(new ApplicationDbContext()))
 8:  
 9: {
 10:  
 11: PasswordValidator = new CustomPasswordValidator(10);
 12:  
 13: }
 14:  
 15: }
  • No change is needed in the AccountController class. Now try to create a user with password that does not meet the requirements. An error message is displayed as shown below.
image

Policy 3: The user cannot reuse the last five previous passwords

When existing users change or reset their password, we can check if they are trying to reuse an old password. This is not done by default since the Identity system does not store the password history per user. We can do this in the application by creating a separate table to store password history per user and then retrieve the password hashes whenever the user tries to reset or change password.
  • Open the IdentityModels.cs file in the editor. Create a new class called ‘PreviousPassword’ as shown below.
 1: public class PreviousPassword
 2:  
 3: {
 4:  
 5: public PreviousPassword()
 6:  
 7: {
 8:  
 9: CreateDate = DateTimeOffset.Now;
 10:  
 11: }
 12:  
 13: [Key, Column(Order = 0)]
 14:  
 15: public string PasswordHash { get; set; }
 16:  
 17: public DateTimeOffset CreateDate { get; set; }
 18:  
 19: [Key, Column(Order = 1)]
 20:  
 21: public string UserId { get; set; }
 22:  
 23: public virtual ApplicationUser User { get; set; }
 24:  
 25: }
In this class, the ‘Password’ field holds the hashed password for the user referenced by the field ‘UserId’. The ‘CreateDate’ holds the timestamp when the password was added. This can be used to filter the password history to get the last 5 passwords or the latest password.
EntityFramework code first creates a corresponding table called ’PreviousPasswords’ with the ‘UserId’ and the ‘Password’ field forming the composite primary key. You can read more about code first conventions here.
  • Add a property on the user class ‘ApplicationUser’ to hold a list of previous passwords
 1: public class ApplicationUser : IdentityUser
 2:     {
 3:         public ApplicationUser()
 4:             : base()
 5:         {
 6:             PreviousUserPasswords = new List<PreviousPassword>();
 7:         }
 8:         public virtual IList<PreviousPassword> PreviousUserPasswords { get; set; }
 9:  
 10:     }
  • As explained in Policy 1, the UserStore class holds the persistence-specific methods for user operations. We need to store the password hash in the history table when the user is created for the first time. Since the UserStore does not define such a method separately, we need to define an override for the existing method to store the password history. This can be done by extending the existing UserStore class and override the existing methods to store the passwords. This is then hooked in the ApplicationUserManager. Add a new class under the IdentityExtensions folder.
 1: public class ApplicationUserStore : UserStore<ApplicationUser>
 2:  
 3: {
 4:  
 5: public ApplicationUserStore(DbContext context)
 6:  
 7: : base(context)
 8:  
 9: {
 10:  
 11: }
 12:  
 13: public override async Task CreateAsync(ApplicationUser user)
 14:  
 15: {
 16:  
 17: await base.CreateAsync(user);
 18:  
 19: await AddToPreviousPasswordsAsync(user, user.PasswordHash);
 20:  
 21: }
 22:  
 23: public Task AddToPreviousPasswordsAsync(ApplicationUser user, string password)
 24:  
 25: {
 26:  
 27: user.PreviousUserPasswords.Add(new PreviousPassword() { UserId = user.Id, PasswordHash = password });
 28:  
 29: return UpdateAsync(user);
 30:  
 31: }
 32:  
 33: }
  • We need to call the above method whenever user tries to change or reset a password. The API’s to do this are defined in the UserManager class. We need to override them and include the above password history method call. We can do this by overriding the ChangePassword and ResetPassword methods in the ApplicationUserManager class defined in the Policy 2.
 1: public class ApplicationUserManager : UserManager<ApplicationUser>
 2:  
 3: {
 4:  
 5: private const int PASSWORD_HISTORY_LIMIT = 5;
 6:  
 7: public ApplicationUserManager()
 8:  
 9: : base(new ApplicationUserStore(new ApplicationDbContext()))
 10:  
 11:  {
 12:  
 13: PasswordValidator = new CustomPasswordValidator(10);
 14:  
 15:  }
 16:  
 17: public override async Task<IdentityResult> ChangePasswordAsync(string userId, string currentPassword, string newPassword)
 18:  
 19: {
 20:  
 21: if (await IsPreviousPassword(userId, newPassword))
 22:  
 23: {
 24:  
 25: return await Task.FromResult(IdentityResult.Failed("Cannot reuse old password"));
 26:  
 27: }
 28:  
 29: var result = await base.ChangePasswordAsync(userId, currentPassword, newPassword);
 30:  
 31: if (result.Succeeded)
 32:  
 33: {
 34:  
 35: var store = Store as ApplicationUserStore;
 36:  
 37: await store.AddToPreviousPasswordsAsync(await FindByIdAsync(userId), PasswordHasher.HashPassword(newPassword));
 38:  
 39: }
 40:  
 41: return result;
 42:  
 43: }
 44:  
 45: public override async Task<IdentityResult> ResetPasswordAsync(string userId, string token, string newPassword)
 46:  
 47: {
 48:  
 49: if (await IsPreviousPassword(userId, newPassword))
 50:  
 51: {
 52:  
 53: return await Task.FromResult(IdentityResult.Failed("Cannot reuse old password"));
 54:  
 55: }
 56:  
 57: var result = await base.ResetPasswordAsync(userId, token, newPassword);
 58:  
 59: if (result.Succeeded)
 60:  
 61: {
 62:  
 63: var store = Store as ApplicationUserStore;
 64:  
 65: await store.AddToPreviousPasswordsAsync(await FindByIdAsync(userId), PasswordHasher.HashPassword(newPassword));
 66:  
 67: }
 68:  
 69: return result;
 70:  
 71: }
 72:  
 73: private async Task<bool> IsPreviousPassword(string userId, string newPassword)
 74:  
 75: {
 76:  
 77: var user = await FindByIdAsync(userId);
 78:  
 79: if (user.PreviousUserPasswords.OrderByDescending(x => x.CreateDate).
 80:  
 81: Select(x => x.PasswordHash).Take(PASSWORD_HISTORY_LIMIT).Where(x => PasswordHasher.VerifyHashedPassword(x, newPassword) != PasswordVerificationResult.Failed
 82:  
 83: ).Any())
 84:  
 85: {
 86:  
 87: return true;
 88:  
 89: }
 90:  
 91: return false;
 92:  
 93: }
 94:  
 95: }
The ‘PASSWORD_HISTORY_LIMIT’ field is used to get the last ‘X’ number of passwords from the table. Notice we hooked in the ApplicationUserStore in the constructor to get the new method to store the password. Whenever the user changes the password, we validate it against the last 5 passwords stored in the table and return true/false based on the validation.
  • Create a local user and go to Manage page. Try to change the password using same password when the user is created. You see a message as shown below.
image

Code Sample

The sample project Identity-PasswordPolicy is checked into http://aspnet.codeplex.com/SourceControl/latest under Samples/Identity.

Summary

This post shows how you can extend the ASP.NET Identity Validation system to enforce different types of password policies for the users of your application. The example uses the MVC template, but the same changes can be applied to a web forms application.
Labels:
 
test