c# - 使用 ASP.NET MVC 5 和 DocumentDB 执行登录操作

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

是否有人成功地创建了一个应用程序,该应用程序执行登录操作并保留来自 asp.net mvc 5 中 DocumentDB 的用户 session 。 我尝试过,但我没有。 我创建了一个模型 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; }
  }

Controller 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");
    }
  }

登录 View 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 是一个从我的 DocumentDB Azure Db 检索信息的存储库(它工作正常)。 当我执行登录时,尽管凭据正确,但它会返回失败。 有谁知道如何帮助我?

最佳答案

我已成功使用此 DocumentDB 提供程序为新的 ASP.NET 身份框架进行 asp.net 身份验证 Link在 git 上,但还没有对其进行大量测试,但从我到目前为止所做的来看,似乎工作得很好。

关于c# - 使用 ASP.NET MVC 5 和 DocumentDB 执行登录操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28155694/

相关文章:

azure - 监视进入 Azure 容器的 Blob 数量

c# - 以编程方式将列和行添加到 WPF Datagrid

c# - 从日期构造时文件名得到不同的文件扩展名

c# - 如何读取本地cookie

asp.net - IQueryable & Repositories - 拿 2 个?

c# - Windows Azure 解决方案的最佳单元测试框架

asp.net-mvc - 调整 Azure Blob 存储中的图像大小

c# - 如何从 ASP.NET 页面获取当前登录的 Windows 帐户?

c# - 从许多不同数据库生成报告的最佳实践

asp.net - 是否应该在 ASP.Net 中的页面加载时检查 session ?