Different login page for every client (on one server) - html

Scenario:
Currently, we have a website tool in asp.net MVC (www.tool.com/login).
Now, the client is looking into customizing the login page for several customers and they will have their own domain (but only one server for all).
for example:
www.client1.com/login,
www.client2.com/login,
www.client3.com/login
Yell at me if this is a stupid question.
But please help and give me an idea on how to implement it.
Should I create different login htmls for each client?

Related to my comment -
Create a Table with ClientInfo in DB:
Title
BackgroundColor
Language
URL --> This has to be unique for this to work [client1, client2]. Don't save the full URL -- Just the Host
Let's say you are passing a Model --> LoginModel to your LoginPage.cshtml
public class LoginModel
{
public string UserName { get; set; }
public string Password { get; set; }
public StyleDTO ClientStyles { get; set; }
}
public class StyleDTO
{
public string Title { get; set; }
public string LogoPath { get; set; }
public string BackGroundColor { get; set; }
}
In your controller when you load View:
public ActionResult Login()
{
var clientUrl = HttpContext.Current.Request.Url.Host;
var cs = dbContext.ClientInfo.First(s => s.Url == clientUrl);
var model = new LoginModel()
{
ClientStyles = new StyleDTO()
{
Title = cs.Title,
LogoPath = cs.LogoPath,
BackGroundColor = cs.BackGroundColor
}
};
}
then in your login View (LoginPage.cshtml):
#model LoginModel
<div style="background-color: #Model.BackGroundColor">
<h1>#Model.Title</h1>
</div>

create a config file with variables for client1, client2, client3
Keep same login page html and based on HTTP_referer pick one of the config variable and render the page based on that.

Related

Lambda Expression - Error Cannot implicitly convert type `vicoapi.Pictures' to `System.Collections.Generic.List<vicoapi.Pictures>' (CS0029)

Hello Together following my Code:
public class SampleData : DropCreateDatabaseIfModelChanges<VicoTvEntities>
{
protected override void Seed(VicoTvEntities context)
{
var pictures = new List<Pictures>{
new Pictures { Name = "Testbild", Url="localhost/test" }
};
var user = new List<User>{
new User{Username="Muster", Password="Pass",Email="max.muster#mustermail.com",Bio="Musterbiografie", Pictures = pictures.Find(pic => pic.Name == "Testbild")}
};
}
}
}
I try to build an API and at the moment, I work on the connection of the DB to the code. I'm following this tutorial: https://www.asp.net/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-4
My Problem is, whenever I try to add the Picture it won't work, because of this converting error. How can I avoid this problem and implement the picture to the User.
My Source Tree looks like this:
Source Tree
The User Class looks as following:
public class User
{
public int IdUser { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public string Bio { get; set; }
public List<Pictures> Pictures { get; set; }
public List<Follow> Following { get; set; }
public List<Follow> Followed { get; set; }
}
The Error I get looks like this:
/Users/username/vicotv-Backend/vicoapi/vicoapi/Models/SampleData.cs(126,126): Error CS0029: Cannot implicitly convert type `vicoapi.Pictures'to System.Collections.Generic.List' (CS0029) (vicoapi)
Although you don't show the class here, I'm fairly certain that your User class has a property defined something like this:
public List<Pictures> Pictures { get; set; }
If that is the case, you need to assign the property to the whole list like this:
user.Pictures = pictures
If you want a user to only be able to have one picture then you need to define the Picture property on the User class like this:
public Pictures Picture { get; set; }
If you define the Picture property like this it should work with your current code.
P.S. I would definitely recommend renaming your "Pictures" class to "Picture". In general you should use the singular form for class names. Have a look at this page for more information.

How to change the attribute display name when returning it as json in mvc?

I have a class
public class ProjectResultClass
{
public double F7 { set; get; }
}
in the controlller i fill it with data and want send it to page
[HttpPost]
public ActionResult CalculateProject(int Project)
{
ProjectResultClass Response= new ProjectResultClass();
Response.F7 = 123.4;
return Json(Response);
}
in the page a get F7: 123.4
I want to give the attribute another name for display.
better use it's to bring the name from resources file
something like
public class ProjectResultClass
{
[DisplayName = "MycustomResourceKey"]
public double F7 { set; get; }
}
try with this code
ProjectResultClass Response = new ProjectResultClass();
Response.F7 = 123.4;
return Json(new { MycustomResourceKey = Response.F7 });

Web Api 2 controller to download wikipedia api and show json output on web

I am trying to parse a wikipedia api which contain the short text of an article.I am using ASP.Net MVC for coding. My wikipedia api is https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=Berlin&redirects= which is in json formatted. At present what I have done is - inside the Model I created a folder named Wiki, and inside it I created four class named Limits.cs, Pageval.cs, Query.cs, Rootobject.cs.
public class Limits
{
public int extracts { get; set; }
}
public class Pageval
{
public int pageid { get; set; }
public int ns { get; set; }
public string title { get; set; }
public string extract { get; set; }
}
public class Query
{
public Dictionary<string, Pageval> pages { get; set; }
}
public class Rootobject
{
public string batchcomplete { get; set; }
public Query query { get; set; }
public Limits limits { get; set; }
}
Now in the controller class I created a WebApi 2 contrller to make the model object show on the web. In this case I am very new in handling this situation because I am new at MVC. I am trying to parse in this way but it is not working at all.
public class WikiController : ApiController
{
// GET: api/Wiki
// GET: api/Wiki/5
public string GetShortText(string name)
{
string result;
using (WebClient client = new WebClient())
{
var response = client.DownloadString("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles=" + name + "&redirects=");
var responseJson = JsonConvert.DeserializeObject<Rootobject>(response);
var firstKey = responseJson.query.pages.First().Key;
var extract = responseJson.query.pages[firstKey].extract;
try
{
Regex regex = new Regex(#".(?<=\()[^()]*(?=\)).(.)");
string.Format("Before:{0}", extract);
extract = regex.Replace(extract, string.Empty);
string result1 = String.Format(extract);
result = Regex.Replace(result1, #"\\n", " ");
}
catch (Exception)
{
result = "Error";
}
}
return result;
}
The Routconfig is-
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
You can do a couple of things. You can use attribute routing or you can define a route for your custom method. The reason it does not map at the moment is that you do not have a route that defines your parameter. So going the route way you can define
routes.MapRoute(
name: "Wiki",
url: "api/wiki/getshorttext/name",
defaults: new { controller = "Wiki", action = "GetShortText", name = UrlParameter.Optional }
)
;
On a side note as you are performing a I/O bound operation I would suggest making your action async using async and await feature of .net. This way you won't block any thread while you waiting for Wikipedia to respond. Also HttpClient offers a DownloadStringAsync which is awaitable. Have a look at async and await

Web API Json Serialization Exception & Dynamic Entities

I have a web API controller method that is returning an object which is giving the client a 500 internal server error. On the server, the output windows says "Newtonsoft.Json.JsonSerializationException". But I cannot see anything wrong with the class I am returning.. and I am sure this has historically been working. Any help would be greatly appreciated!
EDIT: Is this a problem with the web API not being able to serialize a 'dynamic entity'? The code that generates the class is here:
var id = User.Identity.GetUserId();
var user = db.Users
.Where(u => u.Id == id)
.Include(u => u.Friends)
.FirstOrDefault();
return user;
I am returning the following class;
public class User : IdentityUser
{
public User()
{
this.Friends = new List<UserFriend>();
}
public string PhoneNumber { get; set; }
public string Email { get; set; }
public List<UserFriend> Friends { get; set; }
public bool HasRegistered { get; set; }
public string LoginProvider { get; set; }
}
The 'UserFriend' class looks like this;
public class UserFriend
{
public int UserFriendId { get; set; }
public string Id { get; set; }
public string FriendUserId { get; set; }
public string FriendUserName { get; set; }
public string FriendPhoneNumber { get; set; }
}
Strangely, when I hover over the returned object on the server, the type is: {System.Data.Entity.DynamicProxies.User_7283E76A736B4DD47E89120E932CD5C04B62F84C316961F02CDAE3EEF4786504}. I am not sure what this is.. :-O
I used AutoMapper to create a DTO instead of just returning the User class. The DynamicProxies class is because the query uses lazy loading and it has not got the object yet.
After installing automapper (Install-Package AutoMapper);
Mapper.CreateMap<User, UserDto>();
UserDto dto = Mapper.DynamicMap<UserDto>(user);
Then return the dto.

Issue when inserting Chinese data with code first and mysql

I've done a project in ASP.NET MVC 3 using Mysql. To create a database, I use code first ef 4.1. Some of the data I insert in my database is Chinese characters. When I insert the data, it appears with ????. I set my connectionString in my web config like this:
add name="mydbcontext" connectionString="server=localhost;user id=root; password=; database=mydbcontext; pooling=false; charset=utf8;" providerName="MySql.Data.MySqlClient"
But the problem's still there... This is the way I use to insert the data:
public class MyMenu {
public int Id { get; set; }
public string Home { get; set; }
public string About { get; set; }
public string AppForSmartphone { get; set; }
public string Games { get; set; }
public string Softwares { get; set; }
public string Language { get; set; }
public string News { get; set; }
public string Cart { get; set; }
}
public class MyDbContext : DbContext {
public DbSet<MyMenu> MyMenus { get; set; }
public MyDbContext()
: base("MyDbContext") {
}
}
private MyDbContext db = new MyDbContext();
MyMenu tmpmenu = new MyMenu {
About = 關於,
Id = 1,
Cart = 車,
News = 新聞,
AppForSmartphone = 應用程序的智能手機,
Games = 遊戲,
Softwares = 軟件,
Home = 家,
Language = ""
};
db.MyMenus.Add(tmpmenu);
db.SaveChanges();
Can someone help me?
There is no Issue in your code.You just need to check the DataType of the column in which you are saving the data which is in Chinese.I guess you have used varchar() , rather you should be using nvarchar().
I had the same issue a day back. And changing the datatype to nvarchar() fixed my issue.
MySql does support multilingual.
Changing the DataType should fix your issue.
Regards.