双重注册(作为用户/作为公司)

问题描述 投票:0回答:2

我编写了一个新的 ASP.NET MVC 5 应用程序,但在身份验证方面遇到了一些问题。我想要两个注册和登录表单(用于用户和公司)。我使用基本表 ApplicationUser 作为用户,并为公司创建自己的表 CompaniesAccountModel。但当我使用 UserManager 和 SignInManager 时,问题就出现了。我无法修改它们以与 CompaniesAccountModel 一起使用。这是一些代码。

[AllowAnonymous]
public ActionResult CompanyRegister()
{
    return View();
}

//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult CompanyRegister([Bind(Include = "CompanyName, Password, Email, ConfirmPassword")] CompanyAccountModel model)
{
     if (ModelState.IsValid)
     {
         db.CompanyAccountModels.Add(model);
         db.SaveChanges();

         return RedirectToAction("Index", "Home");
     }

     // If we got this far, something failed, redisplay form
     return View(model);
}

[AllowAnonymous]
public ActionResult CompanyLogin(string returnUrl)
{
    ViewBag.ReturnUrl = returnUrl;
    return View();
}

//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> CompanyLogin(CompanyLoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    // 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.CompanyName, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(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);
    }
}

我想使用UserManager和SignInManager进行公司注册和登录。如果有人知道如何做到这一点,那就太好了。

asp.net-mvc asp.net-mvc-4 authentication
2个回答
1
投票

您可以轻松地为公司用户定制身份验证流程。并与普通用户的现有方法一起使用。将此示例视为线索:

public ActionResoult CompanyLogin(CompanyLoginViewModel model, string returnUrl)
{
    // imaging you have own company manager, completely independent from identity
    // you could check validity of company by own preferred logic  
    if(_companyManager.IsValid(model))         
    {
        // company is valid, going to authenticate
        var ident = new ClaimsIdentity(
        new[] 
        {
            // adding following 2 claim just for supporting default antiforgery provider
            new Claim(ClaimTypes.NameIdentifier, model.CompanyName),
            new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),

            // an optional claim you could omit this 
            new Claim(ClaimTypes.Name, model.CompanyName),
            // add this role to differentiate from ordinary users
            new Claim(ClaimTypes.Role, "Company"),                 
            // you could even add some role
            new Claim(ClaimTypes.Role, "AnotherRole"),
            // and so on
        },
        DefaultAuthenticationTypes.ApplicationCookie);

        // Identity is sign in user based on claim don't matter 
        // how you generated it Identity 
        HttpContext.GetOwinContext().Authentication.SignIn(
            new AuthenticationProperties { IsPersistent = false }, ident);

        // auth is succeed, 
        return RedirectToAction("MyAction"); 
     }
     ModelState.AddModelError("", "We could not authorize you :(");
     return View();
} 

由于我们将逻辑注入到 Identity 中,所以我们根本不需要做额外的事情。

[Authorize]
public ActionResult MySecretAction()
{
    // all authorized users could use this method don't matter how has been authenticated
    // we have access current user principal by calling also
    // HttpContext.User
 }

 [Authorize(Roles="Company")]
 public ActionResult MySecretAction()
 {
     // just companies have accesses to this method
 }

此外,如果

ApplicationUser
Company
类有很多共同点,您可以从
Company
扩展
ApplicationUser
。通过这样做,您不需要编写额外的登录方法。相同的登录名适用于两者。但如果出于任何原因你不想从
Company
继承
ApplicationUser
我上面的解决方案更可取。


0
投票

听起来您可能指的是个人在平台或服务上既注册为用户又注册为公司代表的情况。这种双重注册的影响可能会根据具体平台的背景和规则而有所不同。以下是一些一般注意事项:

平台政策:

许多在线平台和服务都有禁止创建多个帐户的政策,特别是如果这样做是为了欺骗或操纵平台。违反这些政策可能会导致帐户被暂停或终止。 用户协议:

当您在平台上注册时,您通常同意服务条款或用户协议。查看这些条款可以深入了解平台有关多次注册的规则。 账户验证:

一些平台可能有验证用户身份或企业账户合法性的程序。双重注册可能会引发额外的审查。 注册目的:

考虑您注册的目的。如果一个帐户用于个人用途,另一个帐户用于商业目的,某些平台可能会允许,但必须遵守其政策。 法律合规性:

确保您的行为符合相关法律法规。在某些情况下,冒充或欺骗行为可能会产生法律后果。 通讯:

如果您不确定是否允许双重注册,或者您是否有合法理由同时拥有两种类型的帐户,请考虑联系平台的支持人员或客户服务以获取澄清。 始终遵守您注册平台的具体规则和指南。如果您确实需要个人和企业帐户,建议您与平台的支持团队公开沟通,并在必要时寻求许可。违反平台规则可能会导致账户被暂停或其他后果。

© www.soinside.com 2019 - 2024. All rights reserved.