Cant use system.web.security and HttpContext.Current.User.Identity.Name in dot net core 6 - asp.net-core-mvc

I'm trying to migrate from ASP.NET MVC on .NET 4.5 to ASP.NET Core 6,
I can't use System.Web.Security any more.
I also can't use HttpContext.Current.User.Identity.Name. Please suggest an alternative to implement the same functionality:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using Ibq.Wages.ManagementSystem.Models;
using Microsoft.AspNetCore.Http;
namespace Ibq.Wages.ManagementSystem.AppStart
{
public class CustomRoleProvider : RoleProvider
{
public int UserCompanyId { get; set; }
public string PayerQid { get; set; }
public string PayerEid { get; set; }
public bool OTPCompleted { get; set; }
public int? AuthorizationLevels { get; set; }
private PIBQWS_prodContext context = new PIBQWS_prodContext();
public override string ApplicationName { get; set; }
public CustomRoleProvider()
{
if(String.IsNullOrEmpty(HttpContext.Current.User.Identity.Name) == false)
UserCompany(HttpContext.Current.User.Identity.Name);
}
}
}

Related

AutoMapperMappingException: Missing type map configuration or unsupported mapping. [.NET Core 3.1]

I've build a WebAPI .NET Core 3.1 using AutoMapper v10.0 and AutoMapper Dependency Injection 8.0.1 and I'm encountering the following error
AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
Role -> RoleResources
Entity.Models.Role -> Entity.Resources.RoleResources
lambda_method(Closure , Role , RoleResources , ResolutionContext )
AutoMapperMappingException: Error mapping types.
Mapping types:
ICollection`1 -> ICollection`1
System.Collections.Generic.ICollection`1[[Entity.Models.Role, Entity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.ICollection`1[[Entity.Resources.RoleResources, Entity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
lambda_method(Closure , ICollection<Role> , ICollection<RoleResources> , ResolutionContext )
I've tired to use .ReverseMap(), Mapthe ICollection directly in the profile with no luck.
Here is my classes
Role.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Entity.Models
{
[Table("Role")]
public class Role
{
[Key]
public Guid Id { get; set; } = Guid.NewGuid();
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public int RoleNumber { get; set; }
public ICollection<RolePrivilage> RolePrivilages { get; set; }
}
}
RoleResources.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Entity.Resources
{
public class RoleResources
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int RoleNumber { get; set; }
}
}
Startup.cs Configure method
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<WhiteLandsDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllers();
services.AddAutoMapper(typeof(Startup));
services.AddScoped<IRoleService, RoleService>();
services.AddScoped<IPrivilageService, PrivilageService>();
services.AddScoped<IRoleRepository, RoleRepository>();
services.AddScoped<IPrivilageRepository, PrivilageRepository>();
}
Mapping Profile
using AutoMapper;
using Entity.Models;
using Entity.Resources;
using System.Collections.Generic;
namespace Core.Mapping
{
public class MappingProfile : Profile
{
public MappingProfile()
{
//CreateMap<ICollection<Role>, ICollection<RoleResources>>();
CreateMap<Role, RoleResources>();
}
}
}
RoleController.cs
[HttpGet]
public async Task<ICollection<RoleResources>> Get()
{
var roles = await _roleService.List();
var resources = _mapper.Map<ICollection<Role>, ICollection<RoleResources>>(roles);
return resources;
}
can you please register your automapper in configuration service like below and give it a try ?
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

Why can't I use the Column data annotation with Entity Framework 5?

I want to use the Column data annotation as shown in the sample code below but the compiler (and also IntelliSense) do not seem to know that particular data annotation. I'm using EF 5 in Visual Studio 2010. I installed EF 5 using NuGet. The Required and MaxLength annotations are working.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Model
{
public class Destination
{
public int DestinationId { get; set; }
[Required]
public string Name { get; set; }
public string Country { get; set; }
[MaxLength(500)]
public string Description { get; set; }
[Column(TypeName="image")]
public byte[] Photo { get; set; }
public List<Lodging> Lodgings { get; set; }
}
}
What am I missing?
Column is in:
using System.ComponentModel.DataAnnotations.Schema;
the following code:
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace ConsoleApplication2
{
public class MyContext : DbContext
{
public IDbSet<Entity> Entities { get; set; }
}
public class Entity
{
public int Id { get; set; }
[Column(TypeName = "image")]
public byte[] Photo { get; set; }
}
}
produces:

Asp.net MVC maxlength not working using data annotation

I am using asp.net mvc 4 i am using [maxlength(2)] in my model but it is not working on client side validation i am new to asp.net mvc .here is my code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace RestrauntsMVC.Models
{
public class Restraunts
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
[MaxLength(2),MinLength(1)]
public int rating { get; set; }
[Required]
public string location { get; set; }
}
}
Answering myself to help future readers.I found out Maxlength and Minlength is for String not for integer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace RestrauntsMVC.Models
{
public class Restraunts
{
public int id { get; set; }
[Required]
public string name { get; set; }
[Required]
[Range(1,10,ErrorMessage="Rating must between 1 to 10")]
public int rating { get; set; }
[Required]
public string location { get; set; }
}
}
Like other mention is only for string so how about writing your own ValidationAttribute ?
using System.ComponentModel.DataAnnotations;
internal class MaxDigitsAttribute : ValidationAttribute
{
private int Max,
Min;
public MaxDigitsAttribute(int max, int min = 0)
{
Max = max;
Min = min;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (!IsValid(value))
{
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
return null;
}
public override bool IsValid(object value)
{
// you could do any custom validation you like
if (value is int)
{
var stringValue = "" + (int)value;
var length = stringValue.Length;
if (length >= Min && length <= Max)
return true;
}
return false;
}
}

Difficulty using "public virtual" variables in database creation

I am creating two tables in Visual Web Developer 2010 Express using the following code:
http://imgur.com/a/Mi2Bv (sorry, the forum will not let me, due to a new account, post pictures directly)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BarApp.Models
{
public class Drinks
{
public int DrinksId { get; set; }
public int EstablishmentsID { get; set; }
public string name { get; set; }
public decimal price { get; set; }
public string description { get; set; }
public string image { get; set; }
public virtual Establishments establishment { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace BarApp.Models
{
public class Promotions
{
public int PromotionsId { get; set; }
public string name { get; set; }
public float discount { get; set; }
public int EstablishmentId { get; set; }
public int DrinkId { get; set; }
public string description { get; set; }
public virtual Establishments establishment { get; set; }
public virtual Drinks drink { get; set; }
}
}
But when I look at the created tables, the Promotions table has actual rows for the public virtual code, whereas the Drinks table does not.
I am able to have the Drinks table function the way I want elsewhere in the project, but I cannot get Promotions to behave the same way because it appears that "public virtual" is giving different results in each table.
I do not understand why my Promotions table is actually creating rows for the public virtual variables. Can someone help me understand?
In this particular situation I was not using the conventional way of referencing the Id field of another table.
I simply needed to change the following:
public int EstablishmentId { get; set; }
public int DrinkId { get; set; }
to the following:
public int EstablishmentsId { get; set; }
public int DrinksId { get; set; }
My tables were created as expected after I made this change.

Accessing objects inside model passed to Razor View in ASP.NET MVC 3

My beer model has a brewery object but I can't access the brewery's properties when I'm in the view. Intellisense seems to think I can access the brewery property but the DisplayFor method doesn't print anything.
#model BeerRecommender.Models.Beer
#{
ViewBag.Title = "Details";
}
<h2>#Html.DisplayFor(model => model.Name)</h2>
<fieldset>
<legend>Details</legend>
<div class="display-label">Style: #Html.DisplayFor(model => model.Style.Name)</div>
<div class="display-label">Brewery: #Html.DisplayFor(model => model.Brewery.Name)</div>
</fieldset>
Here is the Beer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BeerRecommender.Models.ViewModels;
namespace BeerRecommender.Models
{
public class Beer
{
[Key]
public int BeerID { get; set; }
public ICollection<City> Cities { get; set; }
public ICollection<Tag> Tags { get; set; }
public Style Style { get; set; }
public string Name { get; set; }
public Brewery Brewery { get; set; }
}
...and here is the Brewery class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
namespace BeerRecommender.Models
{
public class Brewery
{
[Key]
public int BreweryID { get; set; }
public string Name { get; set; }
public ICollection<Beer> Beers { get; set; }
public City City { get; set; }
}
}
How are you loading the Beer class? If this is from say entity framework you will need to call .Include(o=>o.Brewery) in your loading code to include that table . This is assuming EF 4.1 otherwise you need the string name like .Include("Brewery") if in 4.0

Resources