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

Apache Cordova connecting to Asp.net Web Application in MVC5

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

该站点运行良好,可以处理对我们的数据库表和 blob 存储的数据请求。

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

为了达到这个阶段,我学到了很多东西,但现在我碰壁了,并欣赏一些坚实的东西 advice/guidance。

我正在尝试让我的用户从我的移动应用程序自动登录网站,然后将他们重定向到特定页面。

我用于登录的 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 会话中打开表单操作,因为我无权访问在应用程序中打开的浏览器控件。

此外,如果有人建议我如何编写 Web api 以自动登录到我的 MVC 站点,然后可能获得授权并将数据传回我的移动应用程序,我将不胜感激?

我知道这个问题的范围很广,我很抱歉,我只需要一个立足点来把我推向正确的方向。

我查看了 Azure 移动服务,这似乎是一个不错的选择,但我找不到任何参考来帮助我使用我的 MVC 站点中的现有流程授权我的用户。

非常感谢 suggestions/links 或正确方向的拍打。

@安迪,
这是 Cordova/Phonegap 的常见误解。当您使用 webview(库)进行渲染时,无需使用 CGI。实际上,您可以使用 AJAX 调用或 REST API 执行相同的操作。这意味着您的 MVC 代码虽然漂亮,但完全是浪费。您可以使用 Javascript 和 AJAX.

做您需要的一切

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

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

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

祝你好运