ASP.NET MVC 5通过webservice调用

时间:2015-10-09 14:07:38

标签: c# web-services login asp.net-mvc-5

我正在创建我的第一个ASP.NET MVC网站,并尝试使用Visual Studio 2013创建的模板。我的应用程序不直接访问数据库进行身份验证和所有其他数据库调用。一切都是通过调用传统的SOAP Web服务完成的。此外,登录使用用户名(而不是电子邮件),我认为我已经正确地做了这个改变。

我试图在ApplicationSigninManager中覆盖PasswordSigninAsync来调用我的Web服务并返回相应的结果,如下所示:

        public override Task<SignInStatus> PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout)
    {

        ws.AppsWS webservice = new ws.AppsWS();
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<Parameters><UserName>");
        sb.Append(userName);
        sb.Append("</UserName><Password>");
        sb.Append(password);
        sb.Append("<Password></Parameters>");

        return base.PasswordSignInAsync(userName, password, isPersistent, shouldLockout).ContinueWith(signInStatus =>
        {
            if (signInStatus.Result == SignInStatus.Failure)
            {

                var wsuser = new UserModel();

                var wsresult = webservice.CallAuthenticate(sb.ToString());
                if (wsresult.status.isSuccess)
                {
                    var thisRow = wsresult.dataTable.Rows[0];
                    wsuser.IsAuthenticated = Convert.ToBoolean(thisRow["Authenticated"]);
                    wsuser.UserId= thisRow["UserId"].ToString();
                    wsuser.FirstName = thisRow["FirstName"].ToString();
                    wsuser.LastName = thisRow["LastName"].ToString();
                    wsuser.Email = thisRow["Email"].ToString();
                    if (wsuser.IsAuthenticated)
                    {
                        return SignInStatus.Success;
                    }
                    else
                        return SignInStatus.Failure;

                }
                else
                    return SignInStatus.Failure;
            }

            return signInStatus.Result;
        });

然后在调用函数上,它获取SigninStatus.Success以获取有效的用户名和密码参数。

        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }


        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change to shouldLockout: true
        var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false);

        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

但在_LoginPartial.cshtml中:

@using Microsoft.AspNet.Identity

@if(Request.IsAuthenticated || TempData [“IsAuthenticated”] ==“True”) {

Request.IsAuthenticated是false。如何使IsAuthenticated成为True和持久性,以便我的应用程序知道用户在后续请求中进行了身份验证?

希望你们能指出我如何解决这个问题的正确方向。感谢。

0 个答案:

没有答案