- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
由于有关此过程的文档非常模糊和令人困惑(或陈旧),我想验证我是否正确地执行了操作并且没有遗漏任何步骤。
我正在尝试创建一个安全的登录系统,该系统在浏览器关闭时过期。
-- 在我的 web.config 中我有以下内容 --
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" defaultUrl="Index.aspx" name=".ASPXFORMSAUTH" timeout="100" />
</authentication>
<authorization>
<allow users="?" />
</authorization>
<machineKey decryption="AES" validation="SHA1" validationKey.......... />
所以我有一个带有用户名/密码文本框和这个按钮的登录表单:
<asp:Button ID="LoginButton" runat="Server" OnClick="Login_Authenticate" Text="Sign in" />
在 Login_Authenticate 内部,我执行以下操作:
protected void Login_Authenticate(object sender, EventArgs e){
string userName = UserName.Text;
string password = Password.Text;
bool Authenticated = false;
// Here's code that makes sure that Username and Password is CORRECT
if(AuthClass.Authenticate(userName, password)){
Authenticated = true;
}
// error checking does happen here.
if (Authenticated)
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30), rememberUserName, String.Empty, FormsAuthentication.FormsCookiePath);
string encryptedCookie = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookie);
cookie.Expires = DateTime.Now.AddMinutes(30);
Response.Cookies.Add(cookie);
//FormsAuthentication.RedirectFromLoginPage(userName, false);
Response.Redirect("MainPage.aspx");
}
}
--- 在 MasterPage.master.cs 中,我在 Page_Init() 中进行了以下检查 ---
if (Context.User.Identity.IsAuthenticated)
{
int userid = (int)Session["userid"];
if (userid == null)
{
userid = GetUserID(Context.User.Identity.Name);
if (userid != null)
{
Session["userid"] = userid;
}
}
}
编辑:--- 全局.ASAX ;一些我不太确定的代码是否正确或知道它的作用
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// look if any security information exists for this request
if (HttpContext.Current.User != null)
{
// see if this user is authenticated, any authenticated cookie (ticket) exists for this user
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// see if the authentication is done using FormsAuthentication
if (HttpContext.Current.User.Identity is FormsIdentity)
{
// Get the roles stored for this request from the ticket
// get the identity of the user
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
//Get the form authentication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
//Get the roles stored as UserData into ticket
string[] roles = { };
//Create general prrincipal and assign it to current request
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}
--- 从那时起,在每个页面上,我都使用 Session userid 来收集用户信息和内容,并确保用户具有适当的身份验证和组角色权限。
这一切都正确吗?还是我必须在某处解密任何内容?
这足以使用户登录安全吗?还是我不应该为表单例份验证而烦恼并找到自己的方法来制作自己的 cookie 并自己管理它?
最佳答案
您的代码编写登录的方式将在浏览器 session 中持续存在。这可能有助于了解正在发生的事情的基础知识。
对于基于 cookie 的身份验证方法,实际上有三个操作:
1) 登录 - 验证用户的凭据并在他们的浏览器上创建和存储一个 cookie。
2) 注销 - 只需从浏览器中删除 cookie(通过使 cookie 过期或将其删除)
3) 每个请求验证(即您的 Application_AuthenticateRequest 的部分)- 检查 cookie 是否存在,如果存在,获取用户的身份和角色并设置 HttpContext.Current.User。
通常,FormsAuthentication 模块对您隐藏了其中的大部分内容。看起来您的代码正在尝试使用 FormAuthentication 的某些元素(例如 FormsAuthenticationTicket 和 FormsIdentity)。只要您得到想要的东西就可以了。
您的 Login_Authenticate 方法看起来不错,除了您在 cookie 上设置了过期时间。这将使 cookie 持续存在,即使您关闭并重新打开浏览器也是如此。由于这不是您想要的行为,因此我不会设置 cookie 过期时间。设置此项就像选中“记住我”复选框。
每次从您的应用程序提供页面时,Application_AuthenticateRequest 中的代码都会运行。它的主要工作是设置 HttpContext.Current.User。通常,如果没有用户登录,则 User 为 null 或 Anonymous 用户。如果用户已登录,这应该代表您的用户。
如果您正在做这三件事,那么您可以在代码中的任何位置引用 HttpContext.Current.User 来决定要显示的信息级别。例如,如果您想要将页面限制为仅管理员访问,您可以调用 HttpContext.Current.Users.IsInRole("Administrators"),如果调用返回 false,则将他们重定向到页面之外。
希望这对您有所帮助。
关于c# - Forms Authentication 理解 context.user.identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810496/
我在服务器端实现了 oauth token ,但在无效 token 或 token 过期时,我收到 200 http 状态代码,但在响应正文中我有{"code":"4XX", "data":{"som
我正在尝试将 sinatra-authentication gem 添加到 Sinatra 应用程序中,虽然它在那里并完成了它的一部分工作,但由于某种原因,路由似乎没有被添加。代码基础: requir
我有一个健身移动应用程序的想法,我一直在为 iPhone(基于 Obj-C)、Android(基于 Java)、WebOS(基于 html5)和诺基亚 Qt 开发基于这个想法的应用程序。 我现在需要向
我见过有人使用 UUID 生成身份验证 token 。然而,在 RFC 4122据说 Do not assume that UUIDs are hard to guess; they should n
上下文如下。 pouchdb-authentication API没有为此提供明确的方法。我考虑过使用db.getUser(username [, opts][, callback]) 。然而,该方法
Edge 浏览器中的“基本身份验证”没有保存密码的选项。当浏览器关闭并重新打开时,用户必须重新输入密码。 有没有人解决这个问题? 最佳答案 它仍然存在并且仍在工作,他们只是从那些对话框窗口中删除了复选
嗨,我需要知道如何在 iPhone 上使用 oAuth for twitter 自动登录帐户。该应用程序应登录并向用户显示该帐户的提要。 最佳答案 OAuthentication 需要几个阶段,您可以
Edge 浏览器中的“基本身份验证”没有保存密码的选项。当浏览器关闭并重新打开时,用户必须重新输入密码。 有没有人解决这个问题? 最佳答案 它仍然存在并且仍在工作,他们只是从那些对话框窗口中删除了复选
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
在尝试运行一些 OpenVAS CLI 命令时,我收到 Failed Authentication 错误消息。 OpenVAS 安装在 CentOS 机器上。我尝试使用用户帐户凭据,但仍然收到相同的错
我正在设计一个 web api。我需要让用户对自己进行身份验证。我有点犹豫让用户以明文形式传递他们的用户名/密码.. 类似于:api.mysite.com/auth.php?user=x&pass=y
我尝试通过 oAuth 在 Spring Security 应用程序中验证用户。我已经收到 token 和用户数据。 如何在没有密码和经典登录表单的情况下手动验证用户? 谢谢你。 最佳答案 像这样的东
我正在 Symfony 4 中创建一个简单的登录身份验证系统并使用安全组件 Guard。我的 FormLoginAuthenticator 如下: router = $router;
我正在开发一个具有多个角色的网络应用程序。我想到了一种方法,可以使用 React Router 通过 onEnter 触发器来限制对某些路由的访问。 现在我想知道这是否是防止访问未经授权的页面的可靠方
我已通读 RFC 2617如果支持多种方案,则无法在那里或其他任何地方找到分隔符。例如,假设支持 Basic 和 Digest。我知道它可能会以这种方式出现: HTTP/1.1 401 Unautho
我在 OWIN Cookie 身份验证方面遇到了一些问题。我有一个 .Net 站点,它有一些 MVC 页面,这些页面使用 cookie 身份验证和受不记名 token 保护的 WebAPI 资源。 当
我正在使用 Telnet 向 Mikrotik 路由器发送命令。 telnet 192.168.100.100 -l admin Password: pass1234 [admin@ZYMMA] >
我管理着一个庞大而活跃的论坛,但我们正被一个非常严重的问题所困扰。我们允许用户嵌入远程图像,就像 stackoverflow 处理图像 (imgur) 的方式一样,但是我们没有一组特定的主机,可以使用
这个真的让我抓狂。 我在 JBoss AS 中有一个 Guvnor(稍后会详细介绍)。我编辑了 components.xml 以启用身份验证(使用 JAAS,我已经很好地设置了用户和密码)和基于角色的
我们有一个管理站点,需要身份验证才能访问。站点上的页面包裹在 Coldfusion 自定义标签中,其中包括所有样式和 JS,以及一些其他信息。 我最近制作了一份自定义标签包装器的副本。我将副本放在与原
我是一名优秀的程序员,十分优秀!