【发布时间】:2019-03-11 09:12:31
【问题描述】:
我有一个要求,我使用 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 () {
}
});
}
【问题讨论】:
-
您确定
App是您的控制器名称吗?RedirectToAction参数的顺序是先传递动作名,后传控制器名。 -
@TetsuyaYamamoto:我也尝试了相反的方法,但仍然没有任何反应
-
在你的
ActionResult末尾我发现了return Json(strReturn),这个动作是从AJAX 调用的吗?如果使用AJAX调用,当然不能使用RedirectToAction重定向,需要在客户端脚本中使用location.href和@Url.Action()。 -
@TetsuyaYamamoto:是的,我添加了一个 ajax 调用。另请参阅我更新的问题以获取更多信息
-
@TetsuyaYamamoto:你能告诉我如何使用 mvc 和 ajax。
标签: c# asp.net-mvc model-view-controller windows-authentication