我们有一个网站,它使用ASP标识,并在所有适当的类上散布Authorize属性。
我要做的是为一组特定的操作创建一个单独的身份验证系统。这是一个不是完全匿名的页面,但如果输入正确的PIN,就可以查看。
我开始查看身份验证/授权属性,如果未通过身份验证,它会重定向到我的PIN输入页面。
所以我想我要问的是,我如何认证一个虚拟用户(也就是:不在数据库中),以便在输入正确的PIN后能够访问这些页面?
发布于 2015-06-08 07:38:58
您可以通过继承并覆盖AuthorizeCore方法来创建自己版本的AuthorizeAttribute。
public class PinAuthorizeAttribute : AuthorizeAttribute
{
private readonly string _password;
public PinAuthorizeAttribute(string password)
{
_password = password;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//Check if user has entered the correct PIN, perhaps
//by keeping the PIN in the Session
if(Session["PIN") == _password)
return true;
return false;
}
}现在将它添加到您的操作方法中:
[PinAuthorize("1234")]
public ActionResult ProtectedIndex()
{
//snip
}https://stackoverflow.com/questions/30699462
复制相似问题