我有一个要求,在此我要使用Windows身份验证来验证用户。到那里为止,它工作得很好。但是,当我尝试重定向到各自的controllers
时,它什么也没做。
下面是我的代码。
public ActionResult ValidateUser(string strUsername, string strPassword)
{
string strReturn = "";
string strDbError = string.Empty;
strUsername = strUsername.Trim();
strPassword = strPassword.Trim();
UserProviderClient ObjUMS = new UserProviderClient();
bool result = ObjUMS.AuthenticateUser(strUsername, strPassword, out strDbError);
Session["isUserAuthenticated"] = result;
if (result == true)
{
Session["isUserOutsideINDomain"] = true;
Session["OutsideINDomainUsername"] = strUsername;
//redirect to respective controller
UMS ObjUMSDATA = new UMS();
string strUserName = "";
string strCurrentGroupName = "";
int intCurrentGroupID = 0;
strUserName = System.Web.HttpContext.Current.User.Identity.Name.Split('\\')[1];
_UMSUserName = strUserName;
if (!string.IsNullOrEmpty(strUserName))
{
List<UMSGroupDetails> lstUMSGroupDetails = null;
List<UMSLocationDetails> lstUMSLocationDetails = null;
ObjUMSDATA.GetUMSGroups(strUserName, out strCurrentGroupName, out intCurrentGroupID, out lstUMSLocationDetails, out lstUMSGroupDetails);
if (strCurrentGroupName != "" && intCurrentGroupID != 0)
{
ViewBag.LoginUserName = strUserName.ToUpper();
ViewBag.CurrentGroupName = strCurrentGroupName;
ViewBag.CurrentGroupID = intCurrentGroupID;
ViewBag.GroupDetails = lstUMSGroupDetails;
ViewBag.LocationDetails = lstUMSLocationDetails;
TempData["Location"] = lstUMSLocationDetails;
TempData["strCurrentGroupName"] = strCurrentGroupName;
TempData.Keep();
if (strCurrentGroupName == "NEIQC_SAP_ENGINEER")
{
return RedirectToAction("Assign","App"); // here its not redirecting properly.
}
else if (strCurrentGroupName == "NEIQC_FIBER_ENGINEER")
{
return RedirectToAction("App", "Certify");
}
else if (strCurrentGroupName == "NEIQC_CMM")
{
return RedirectToAction("App", "Approver");
}
}
else
{
return RedirectToAction("ErrorPage", "UnAuthorize");
}
}
}
else
{
strReturn = "Login UnSuccessful";
}
return Json(strReturn);
}
请帮助为什么它不起作用
更新
我的路线配置详细信息。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Assign",
url: "{controller}/{action}/{id}",
defaults: new { controller = "App", action = "Assign", id = UrlParameter.Optional }
);
}
Ajax通话
function validateUser() {
var getUserName = $('#txtUsername').val();
var getPassword = $('#txtPassword').val();
// console.log(getUserName);
//console.log(getPassword);
var Values = { "strUsername": getUserName, "strPassword": getPassword };
$.ajax({
url: AppConfig.PrefixURL + "Home/ValidateUser",
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(Values),
processData: false,
success: function () {
},
error: function () {
}
});
}
答案 0 :(得分:0)
您的路由配置无法区分您给定的两条路由,因为它们没有区别。另外,您的默认路由应始终位于末尾。请将路由配置更改为此:
注意,我更改了“分配”路由的网址,并在末尾移动了“默认”路由。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Assign",
url: "App/{action}/{id}",
defaults: new { controller = "App", action = "Assign", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
);
}
答案 1 :(得分:0)
由于您正在使用AJAX调用控制器操作,因此显然RedirectToAction
在那里不起作用,因为AJAX调用旨在保留在同一页面中。您应该为所有响应返回JSON字符串,并在客户端脚本上使用switch...case
块或if条件来重定向到相应的动作,并使用location.href
和@Url.Action()
助手来生成URL字符串:< / p>
[HttpPost]
public ActionResult ValidateUser(string strUsername, string strPassword)
{
string strReturn = "";
string strDbError = string.Empty;
strUsername = strUsername.Trim();
strPassword = strPassword.Trim();
UserProviderClient ObjUMS = new UserProviderClient();
bool result = ObjUMS.AuthenticateUser(strUsername, strPassword, out strDbError);
Session["isUserAuthenticated"] = result;
if (result == true)
{
Session["isUserOutsideINDomain"] = true;
Session["OutsideINDomainUsername"] = strUsername;
//redirect to respective controller
UMS ObjUMSDATA = new UMS();
string strUserName = "";
string strCurrentGroupName = "";
int intCurrentGroupID = 0;
strUserName = System.Web.HttpContext.Current.User.Identity.Name.Split('\\')[1];
_UMSUserName = strUserName;
if (!string.IsNullOrEmpty(strUserName))
{
List<UMSGroupDetails> lstUMSGroupDetails = null;
List<UMSLocationDetails> lstUMSLocationDetails = null;
ObjUMSDATA.GetUMSGroups(strUserName, out strCurrentGroupName, out intCurrentGroupID, out lstUMSLocationDetails, out lstUMSGroupDetails);
if (strCurrentGroupName != "" && intCurrentGroupID != 0)
{
ViewBag.LoginUserName = strUserName.ToUpper();
ViewBag.CurrentGroupName = strCurrentGroupName;
ViewBag.CurrentGroupID = intCurrentGroupID;
ViewBag.GroupDetails = lstUMSGroupDetails;
ViewBag.LocationDetails = lstUMSLocationDetails;
TempData["Location"] = lstUMSLocationDetails;
TempData["strCurrentGroupName"] = strCurrentGroupName;
TempData.Keep();
// here you need to return string for success result
return Json(strCurrentGroupName);
}
else
{
return Json("Error");
}
}
}
else
{
strReturn = "Login UnSuccessful";
}
return Json(strReturn);
}
AJAX通话
$.ajax({
url: AppConfig.PrefixURL + "Home/ValidateUser",
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(Values),
processData: false,
success: function (result) {
switch (result) {
case 'NEIQC_SAP_ENGINEER':
location.href = '@Url.Action("Assign", "App")';
break;
case 'NEIQC_FIBER_ENGINEER':
location.href = '@Url.Action("Certify", "App")';
break;
case 'NEIQC_CMM':
location.href = '@Url.Action("Approver", "App")';
break;
case 'Error':
location.href = '@Url.Action("ErrorPage", "UnAuthorize")';
break;
case 'Login UnSuccessful':
// do something
break;
// other case options here
}
},
error: function (xhr, status, err) {
// error handling
}
});
附加说明:
RouteConfig
处理从最特定的路由到更通用的路由,因此Default
路由必须在最后声明,并且自定义路由定义必须与默认路由不同。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Assign",
url: "App/{action}/{id}",
defaults: new { controller = "App", action = "Assign", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Login", id = UrlParameter.Optional }
);
}
答案 2 :(得分:0)
所以您当前有一个像这样的ajax调用:
$.ajax({
url: AppConfig.PrefixURL + "Home/ValidateUser",
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(Values),
processData: false,
success: function () {
},
error: function () {
}
});
这很好,但是目前成功不起作用。您有一个返回字符串的控制器方法
return Json(strReturn);
现在,我希望您不要返回该值,而是将角色作为字符串返回。
我希望您的ajax类似:
$.ajax({
url: AppConfig.PrefixURL + "Home/ValidateUser",
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify(Values),
processData: false,
success: function (result) {
//Here we will be returning the role as a string from the first ajax call and using it to check against in here
//I just want you to essentiatly do the if statements in the controller here such as this
if(result == "NEIQC_SAP_ENGINEER")
{
window.location.href = '@url.Action("Assign", "App")'
}
},
error: function () {
}
});
编辑:这个答案似乎与@TetsuyaYamamoto的非常相似。两者都应该起作用,但是是否使用switch语句的偏好完全取决于您