Apache Cordova在MVC5中连接到Asp.net Web应用程序

Apache Cordova connecting to Asp.net Web Application in MVC5

本文关键字:Asp net Web 应用程序 连接 Cordova MVC5 Apache      更新时间:2023-09-26

我目前有一个ASP。. NET Web应用程序,使用MVC 5,启用asp.net-identify和用户角色。

网站运行良好,处理数据请求到我们的数据库表和blob存储

我们有一个用Apache Cordova Visual Studio 2015编写的移动应用程序,它实现了蓝牙LE,一切正常工作。

我学到了很多东西才走到这一步,但现在我遇到了瓶颈,我很感激一些可靠的建议/指导。

我正试图最初从我的移动应用程序自动登录我的用户到网站,并将它们重定向到特定的页面。

我的MVC站点登录代码如下:

    //
    // GET: /Account/AppLogin
    [AllowAnonymous]
    public ActionResult AppLogin()
    {
        ViewBag.ReturnUrl = "not used";
        return View();
    }
    //
    // POST: /Account/AppLogin
    [HttpPost]
    [AllowAnonymous]
    //[ValidateAntiForgeryToken]  <-- Commented out to try to gain access from Cordova.
    public async Task<ActionResult> AppLogin(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }
        // Require the user to have a confirmed email before they can log on.
        // var user = await UserManager.FindByNameAsync(model.Email);
        var user = UserManager.Find(model.Email, model.Password);
        if (user != null)
        {
            if (!await UserManager.IsEmailConfirmedAsync(user.Id))
            {
                string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Account Registration - Confirm your account-Resend");
                // Uncomment to debug locally  
                // ViewBag.Link = callbackUrl;
                ViewBag.errorMessage = "You must have a confirmed email to log on. "
                                     + "The confirmation email has been resent to your email account.";
                return View("Error");
            }
            var currentUser = UserManager.FindByName(model.Email);
            bool roleresult = UserManager.IsInRole(currentUser.Id, "Customer");
            if (roleresult == false)
            {
                ViewBag.errorMessage = "You do not have Customer Account Accesss for this site. "
                                        + "Please contact the Support Team.";
                return View("Error");
            }
        }
        // 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.Email, model.Password, model.RememberMe, shouldLockout: false);
        switch (result)
        {
            case SignInStatus.Success:
                return AppRedirectToLocal(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);
        }
    }
    private ActionResult AppRedirectToLocal(string returnUrl)
    {
        return RedirectToAction("Index", "Customer");
    }

我的视图代码如下。

    <section id="loginForm">
        @using (Html.BeginForm("AppLogin", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
            @Html.AntiForgeryToken()
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                <div class="col-md-10">
                    @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                    @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <div class="checkbox">
                        @Html.LabelFor(m => m.RememberMe)<text> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</text>
                        @Html.CheckBoxFor(m => m.RememberMe)
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span> Log In</button>
                </div>
            </div>
            <p>
                @Html.ActionLink("Register as a new user", "Register")
            </p>
                <p>
                    @Html.ActionLink("Forgot your password?", "ForgotPassword")
                </p>
        }
    </section>

我的Apache Cordova代码如下:

     <form action="https://nameofmywebsite.azurewebsites.net/Account/AppLogin/" class="form-horizontal" method="post" role="form">
                <input name="__RequestVerificationToken" type="hidden" value="" />   <!-- Left this code in but do not think it is required.  -->
         <hr />
                        <input id="Email" name="Email" type="text" value="andy@galleos.co.uk" />
                        <input id="Password" name="Password" type="password" value="Andy.1234" />
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            <label for="RememberMe">Remember me?</label><text> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</text>
                            <input data-val="true" data-val-required="The Remember me? field is required." id="RememberMe" name="RememberMe" type="checkbox" value="true" />
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-ok"></span> Log In</button>
                    </div>
                </div>
                <p>
                    <a href="/Account/Register">Register as a new user</a>
                </p>
                <p>
                    <a href="/Account/ForgotPassword">Forgot your password?</a>
                </p>
            </form>

我现在可以看到用户名和密码传递给表单并正确填写。

关键问题,我想在cordova.InAppBrowser.open会话中打开表单Action,因为我无法访问应用程序内的浏览器控件打开。

我也非常感谢任何人建议我如何编写一个web api自动登录到我的MVC网站,然后潜在地获得授权,并将数据传递到我的移动应用程序?

我知道这个问题的范围很广,我很抱歉,我只是需要一个人帮我把正确的方向推一下。

我已经研究了Azure移动服务,这似乎是一个很好的方法,但是我找不到任何参考来帮助我授权我的用户使用我的MVC站点内的现有进程。

非常感谢您的建议/链接或拍打正确的方向。

@Andy,
这是Cordova/Phonegap的常见误解。当你使用webview(库)来做渲染时,没有必要使用CGI。实际上,您可以通过AJAX调用或REST API实现相同的操作。这意味着你的MVC代码虽然漂亮,但完全是浪费。你可以用Javascript和AJAX做任何你需要的事情。

我建议你学习AJAX,因为虽然Javascript对大多数程序员来说是陌生的,但一旦他们理解了它,他们就掌握了它。

微软应该有类似的东西
https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started

最后一点,AJAX系统是异步的,而不是同步的。这意味着系统是事件驱动的,而不是线性的。如果您需要明确异步,请询问。

祝你好运