[英]Apache Cordova connecting to Asp.net Web Application in MVC5
我目前有一個使用MVC 5並啟用了asp.net-identify和用戶角色的ASP.NET Web應用程序。
該站點運行良好,正在處理對數據庫表和Blob存儲的數據請求。
我們有一個用Apache Cordova Visual Studio 2015編寫的移動應用程序,該應用程序實現了藍牙LE的所有功能都可以正常工作。
我已經學到了大量知識,可以進入這個階段,但是現在我遇到了麻煩,並感謝一些可靠的建議/指導。
我正在嘗試首先將我的用戶從我的Mobile App自動登錄到網站,然后將他們重定向到特定頁面。
我的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> </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> </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網站中的現有過程來授權用戶。
非常感謝您的建議/鏈接或正確方向的耳光。
@安迪
這是Cordova / Phonegap的常見誤解。 當您使用Web視圖(庫)進行渲染時,無需使用CGI。 實際上,您可以使用AJAX調用或REST API進行相同的操作。 這意味着您的MVC代碼雖然漂亮,但卻是完全的浪費。 您可以使用Javascript和AJAX來完成所需的一切。
我建議您學習AJAX,因為盡管Javascript對於大多數程序員來說都是陌生的,但是一旦他們理解了,他們就會掌握它。
Microsoft應該具有與此等效的功能
https://developer.mozilla.org/zh-CN/docs/AJAX/Getting_Started
最后一件事,AJAX系統是異步的 ,而不是同步的。 這意味着系統是事件驅動的 ,不是線性的。 如果您需要了解“ 異步” ,請詢問。
祝你好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.