Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have the following code in my controller for login in my MVC. I am trying to use different users to be able to login and be redirected to different pages on login. When I use the code below I get the following error:

Value cannot be null.
Parameter name: providerUserKey



How do I correct this?

JavaScript
private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private MembershipUser CurrentUser;
private string[] UserRoles;
private FxDataClassesDataContext dcfx = new FxDataClassesDataContext();
private object UserName;
//
// GET: /Account/Index
////private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public ActionResult Index()
{

return View();
}

//
// GET: /Account/LogOn

[AllowAnonymous]
public ActionResult LogOn()
{
return View();
}

[AllowAnonymous]
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{

MembershipUser currentUser = Membership.GetUser(UserName);
string UserID = currentUser.ProviderUserKey.ToString();

UserRoles = Roles.GetRolesForUser();

if (UserRoles.Length > 0)
{
if (User.IsInRole("Shipper"))
{
// Find Shipper data
var objShipper = (from s in dcfx.FxShippers
where s.Shipper_USerName == CurrentUser.UserName
select s).First();

Session.Add("ShipperID", objShipper.Shipper_ID);

if (log.IsInfoEnabled)
{
log.Info("Username: " + CurrentUser.UserName + ", Role: " + UserRoles[0] + ", Shipper ID: " + objShipper.Shipper_ID.ToString());
}

// Redirect to Shipper Portal page
Response.Redirect("Shipperportal.cshtml");
}
else if (UserRoles[0] == "Transporter")
{
// Find Transporter data
// Find Shipper data
var objTransporter = (from s in dcfx.FxTransporters
where s.Transporter_UserName == CurrentUser.UserName
select s).First();

Session.Add("TransporterID", objTransporter.Transporter_ID);

if (log.IsInfoEnabled)
{
log.Info("Username: " + CurrentUser.UserName + ", Role: " + UserRoles[0] + ", Transporter ID: " + objTransporter.Transporter_ID.ToString());
}

// Redirect to Transporter Portal page
Response.Redirect("TransporterPortal.cshtml");
}
else if (UserRoles[0] == "FxAdmin")
{
// Redirect to Transporter Portal page
Response.Redirect("");
}

return View();

}
Posted
Updated 17-Apr-12 21:26pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900