【问题标题】:Custom Authentication .Net MVC自定义身份验证 .Net MVC
【发布时间】:2016-02-21 08:26:49
【问题描述】:

我有一个允许用户支付假期预订费用的网络应用程序。

用户将使用唯一的预订 ID 和预订密码的组合登录。

有没有一种简单的方法可以在用户成功登录后对其进行身份验证。为此目的使用 .net 会话对象是个好主意吗?

该应用不会进行负载平衡,因此我无需担心跨不同机器存储会话。

我不想使用 .Net 成员资格,因为我不想在数据库中创建任何额外的表。

【问题讨论】:

    标签: asp.net-mvc authentication asp.net-mvc-5


    【解决方案1】:

    如果您能够检索用户,我想这是可能的。以下是我会尝试的方式:

    public ActionResult UrlAuth(string bookingId, string bookingPass)
    {
        var userManager=HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
    
        // Retrieve the user by the give booking values (Custom part in your app
        string userId= _bookingRepository.GetUserIdByBooking(bookingId, bookinggPass);
    
        var user = userManager.FindById(userId);
        if (user != null)
        {
            var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
    
            HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = false }, userIdentity );
    
            // authentication succeed do what you want 
            return Redirect("Wherever");
        }
    
        ModelState.AddModelError("", "Invalid booking values");
    
        return View();
    }
    

    【讨论】:

    • 谢谢。这很有帮助
    【解决方案2】:
    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多