Asp.net mvc 自定义身份验证.NETMVC

Asp.net mvc 自定义身份验证.NETMVC,asp.net-mvc,authentication,asp.net-mvc-5,Asp.net Mvc,Authentication,Asp.net Mvc 5,我有一个web应用程序,允许用户支付假日预订费用 用户将使用唯一的预订ID和预订密码组合登录 是否有一种简单的方法可以在用户成功登录后对其进行身份验证。为此目的使用.net会话对象是一个好主意吗 这个应用程序不会进行负载平衡,所以我不需要担心在不同的机器上存储会话 我不想使用.Net成员身份,因为我不想在数据库中创建任何额外的表。如果您能够检索用户,我想这是可能的。以下是我尝试的方式: public ActionResult UrlAuth(string bookingId, string bo

我有一个web应用程序,允许用户支付假日预订费用

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

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

这个应用程序不会进行负载平衡,所以我不需要担心在不同的机器上存储会话


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

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

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();
}
public ActionResult UrlAuth(字符串bookingId,字符串bookingPass)
{
var userManager=HttpContext.GetOwinContext().GetUserManager();
//通过给定预订值(应用程序中的自定义部分)检索用户
字符串userId=\u bookingRepository.GetUserIdByBooking(bookingId,bookinggPass);
var user=userManager.FindById(userId);
如果(用户!=null)
{
var userIdentity=userManager.CreateIdentity(用户,DefaultAuthenticationTypes.ApplicationOkie);
HttpContext.GetOwinContext().Authentication.SignIn(新的AuthenticationProperties{IsPersistent=false},userIdentity);
//你想干什么就干什么
返回重定向(“无论何处”);
}
ModelState.AddModelError(“,”无效预订值”);
返回视图();
}

这是一篇关于MVC自定义身份验证的好文章


谢谢。这帮了大忙