1. C# Styles.Render
if your folder path isContent/Kendo
and if you use render as below
@Styles.Render("~/Content/Kendo")
It will not load. Because path and folder name is same.
You have to use different name like below
@Styles.Render("~/Content/Kendocss")
2. MVC Identity add custom role
public void Configuration(IAppBuilder app)
{
{
ConfigureAuth(app);
createRolesandUsers();
}
createRolesandUsers();
}
//In this method we will create default User roles and Admin user for login
private void createRolesandUsers()
{
ApplicationDbContext context = new ApplicationDbContext();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
//In Startup iam creating first Admin Role and creating a default Admin User
if (!roleManager.RoleExists("Admin"))
{
//first we create Admin rool
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Admin";
roleManager.Create(role);
//Here we create a Admin super user who will maintain the website
var user = new ApplicationUser();
user.UserName = "UserName";
user.Email = "sample@gmail.com";
string userPWD = "12345";
var chkUser = UserManager.Create(user, userPWD);
//Add default User to Role Admin
if (chkUser.Succeeded)
{
var result1 = UserManager.AddToRole(user.Id, "Admin");
}
}
//creating Creating Manager role
if (!roleManager.RoleExists("Manager"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Manager";
roleManager.Create(role);
}
//creating Creating Employee role
if (!roleManager.RoleExists("Employee"))
{
var role = new Microsoft.AspNet.Identity.EntityFramework.IdentityRole();
role.Name = "Employee";
roleManager.Create(role);
}
}
3.Write Custom AuthorizeAttribute
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
{
private readonly string[] allowedroles;
public CustomAuthorizeAttribute(params string[] roles)
{
this.allowedroles = roles;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
bool authorize = false;
var role = (((ClaimsIdentity)httpContext.User.Identity).Claims.FirstOrDefault(x => x.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/role")).Value;
foreach (var roles in allowedroles)
{
if (roles == role)
{
authorize = true; /* return true if Entity has current user(active) with specific role */
}
}
return authorize;
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
4. Pass string to date time without formatting
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);5. Add log
private static void WriteLog_2(string error)
{
try
{
StringBuilder sb = new StringBuilder();
var LogName = "logsFormServices-" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
sb.Append(DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss") + " | " + error + Environment.NewLine);
File.AppendAllText("C:" + "\\" + LogName, sb.ToString());
sb.Clear();
}
catch (Exception)
{
}
}
6.Call powershell script inside C# code
private bool CheckPrinterName(string Name)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("$U ='UserName';" +
"$P = ConvertTo-SecureString 'Password' -AsPlainText -Force;" +
"$mycreds = New-Object System.Management.Automation.PSCredential($U, $P);" +
"Set-Item -Path WSMan:\\localhost\\Client\\TrustedHosts -Value '192.168.xx.xx' -Force;" +
"Invoke-Command -ComputerName '" + TestContext.Properties["CVPServerIP"].ToString() +"' -ScriptBlock { Get-Printer } -credential $mycreds");
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
if (PowerShellInstance.Streams.Error.Count > 0)
{
return false;
}
var IsFound = false;
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
if (outputItem.BaseObject.ToString().Contains(Name))
{
IsFound = true;
break;
}
}
}
return IsFound;
}
}
7.Multi Thread
class Program
{
static void Main(string[] args)
{
Parallel.For(0, 10, (i) =>
{
Console.WriteLine("Parent Number****************** " + i);
Run(i);
});
}
private static void Run(int ID)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Parent ID:" + ID + "--> Child Number:" + i);
}
}
}
Sample out put

8.Call store procedure in C#
string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["ConStr"];
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("AddRecord", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@JobId", SqlDbType.NVarChar).Value = Guid.NewGuid().ToString();
cmd.Parameters.AddWithValue("@Name", SqlDbType.NVarChar).Value = "12345";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
No comments:
Post a Comment