使用带有DocumentDB的ASP.NET MVC 5执行登录操作

时间:2015-01-26 17:45:18

标签: c# asp.net azure asp.net-mvc-5 azure-cosmosdb

是否有人设法创建一个执行登录操作的应用程序,并在asp.net mvc 5中保留来自DocumentDB的用户会话。 我试过了,但我没有。 我创建了一个模型MyUser.cs

public class MyUser
  {

    [JsonProperty(PropertyName = "id")]
    public string id { get; set; }

    [JsonProperty(PropertyName = "mail")]
    public string mail { get; set; }

    [JsonProperty(PropertyName = "password")]
    public string password { get; set; }
  }

控制器MyUserController

public class MyUserController : Controller
  {
    public ActionResult Index()
    {
      var client = DocumentDBRepository.InstantiatesClient();
      var database = DocumentDBRepository.ReadOrCreateDatabase("myDB");
      var collection = DocumentDBRepository.ReadOrCreateCollection(database.SelfLink, "myUsers");
      List<Server_MVC5.Models.MyUser> users = DocumentDBRepository.getMyUsers(collection);
      return View(users);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login([Bind(Include = "email,password")] Models.MyUser myuser)
    {
      if (ModelState.IsValid)
      {
        DocumentDBRepository.Login(myuser.mail, myuser.password);
        return RedirectToAction("Index");
      }

      return View(myuser);
    }

    public ActionResult Login(string email, string password)
    {
      if (email == null)
      {
        return HttpNotFound();
      }

      Models.MyUser myuser = DocumentDBRepository.Login(email, password);
      if (myuser == null)
      {
        return HttpNotFound();
      }

      return View("Index");
    }
  }

登录Login.cshtml的视图

@using Server_MVC5.Models
@model LoginViewModel
@{
    ViewBag.Title = "Log in";
}

<h2>@ViewBag.Title.</h2>
<div class="row">
    <div class="col-md-8">
        <section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <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.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>
                <p>
                    @Html.ActionLink("Register as a new user", "Register")
                </p>
                @* Enable this once you have account confirmation enabled for password reset functionality
                    <p>
                        @Html.ActionLink("Forgot your password?", "ForgotPassword")
                    </p>*@
            }
        </section>
    </div>
    <div class="col-md-4">
        <section id="socialLoginForm">
            @Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
        </section>
    </div>
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

我声明DocumentDBRepository是一个repo,它从我的DocumentDB Azure Db中检索信息(它可以正常工作)。 当我执行登录时,尽管凭据是正确的,它仍会返回失败。 有谁知道如何帮助我?

1 个答案:

答案 0 :(得分:2)

我已经设法使用这个DocumentDB提供程序在git上使用新的ASP.NET身份框架Link进行asp.net身份验证,但是还没有对它进行大量测试,但似乎工作得很好到目前为止我已经完成了。