如何判断用户是否已登录? [英] How do I tell if the user if logged in?

查看:109
本文介绍了如何判断用户是否已登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我只是计划一个新的ASP.NET站点,并想知道处理以下常见场景的最佳方式

.. 。


该网站提供注册用户可以登录的选项,并且可以使用

访问额外的功能。登录表单是一个简单的用户名/密码

事件,如果他们还没有登录,则会出现在每个页面的边缘。如果他们被记录在,他们的用户名将显示在

,并带有一个退出按钮。


现在,假设他们尝试登录。登录表单发布在

页面的日志中,用于验证用户名和密码。如果正确,他们现在已经登录了。如果没有,他们会被告知并再次询问详细信息,

或要求注册。


如何在网站的其他部分处理此问题?我需要在某些页面上更改

显示,以便像如果你已登录你

那样的消息能够做到......。将被替换为额外的

功能的链接。同样,某些页面如果没有登录则无法使用。


在经典ASP中,这只是设置一个Session变量的问题,比如说

"用户名"非空白。你可以有一个小的包含文件,在每个相关的页面上提取

,其中包含用于检查Session

变量的代码,然后将它们重定向离开页面在没有登录的情况下允许没有b $ b。如果它是一个他们可以看到的页面,

你会做类似的事情......


<%如果会话(" Username")<>""然后%>

< p>点击< a href =" comment.asp"> here< / a>添加评论。< / p>

<%Else%>

< p>如果您已登录,则可以添加评论。< ; / p>

<%End如果%>


现在我认为在ASP中不允许这样的在线编码。 NET,所以

如何处理这种情况?


TIA


-

Alan Silver

(此行下面添加的任何内容都与我无关)

解决方案

嗨Alan,


在.NET页面中,您可以将超链接控件和Label控件放在相同的

位置。然后在codebehind中按照登录方式切换它们


如果登录则

Hyperlink.Visible = True

Label.Visible =错误

否则

Hyperlink.Visible = False

Label.Visible = True

结束如果

HTH


Elton Wang
el **** ****@hotmail.com

" Alan Silver"写道:

您好,

我只是计划一个新的ASP.NET站点,并想知道处理以下常见问题的最佳方法场景......

该网站提供注册用户可以登录的选项,并且可以访问额外的功能。登录表单是一个简单的用户名/密码
如果他们还没有登录就会显示在每个页面的边缘。如果他们已登录,他们的用户名将会显示用
按钮退出。

现在,假设他们尝试登录。登录表单会发布到
页面的日志中,该页面会验证用户名和密码。如果正确,他们现在已登录。如果没有,他们会被告知并再次询问详细信息,
或要求注册。

我该如何处理这个问题?在网站的其余部分?我需要改变某些页面上的
显示,以便像如果你已经登录了你
那样的消息能够做到......。将被替换为额外
功能的链接。同样,如果某些页面未登录,则显然不可用。

在Classic ASP中,这只是设置Session变量的问题,比如
Username。非空白。您可以在每个相关页面上打开一个小的包含文件,其中包含用于检查Session
变量的代码,如果它们不是,则会将它们从页面重定向到
允许那里没有登录。如果它是一个他们可以看到的页面,
你会做类似的事情......

<%If Session(" Username")<> ;""然后%>
< p>点击< a href =" comment.asp"> here< / a>添加评论。< / p>
<%Else%>
< p>如果您已登录,则可以添加评论。< / p>
<%End if%>

现在我认为在ASP.NET中不允许像这样的内联编码,所以
如何处理这种情况呢? />
TIA

- Alan Silver
(此行下面添加的任何内容都与我无关)



>您好Alan,


在.NET页面中,您可以将Hyperlink控件和Label控件放在相同的位置。然后在codebehind中根据登录切换它们或不是


谢谢,这是有道理的。什么是处理实际日志的最佳方式

in?你有一个变量登录。那套在哪里?请记住

登录将在一个页面上完成,然后我需要将

信息提供给他们之后访问的每个页面。我可以

了解一个Session变量,但有更好的方法吗?


感谢您的回复。

如果登录然后
Hyperlink.Visible = True
Label.Visible = False
其他
Hyperlink.Visible = False
Label.Visible = True
结束如果
HTH

Elton Wang
el********@hotmail.com

" Alan Silver"写道:

您好,

我只是计划一个新的ASP.NET站点,并想知道处理以下常见问题的最佳方法场景......

该网站提供注册用户可以登录的选项,并且可以访问额外的功能。登录表单是一个简单的用户名/密码
如果他们还没有登录就会显示在每个页面的边缘。如果他们已登录,他们的用户名将会显示用
按钮退出。

现在,假设他们尝试登录。登录表单会发布到
页面的日志中,该页面会验证用户名和密码。如果正确,他们现在已登录。如果没有,他们会被告知并再次询问详细信息,
或要求注册。

我该如何处理这个问题?在网站的其余部分?我需要改变某些页面上的
显示,以便像如果你已经登录了你
那样的消息能够做到......。将被替换为额外
功能的链接。同样,如果某些页面未登录,则显然不可用。

在Classic ASP中,这只是设置Session变量的问题,比如
Username。非空白。您可以在每个相关页面上打开一个小的包含文件,其中包含用于检查Session
变量的代码,如果它们不是,则会将它们从页面重定向到
允许那里没有登录。如果它是一个他们可以看到的页面,
你会做类似的事情......

<%If Session(" Username")<> ;""然后%>
< p>点击< a href =" comment.asp"> here< / a>添加评论。< / p>
<%Else%>
< p>如果您已登录,则可以添加评论。< / p>
<%End if%>

现在我认为在ASP.NET中不允许像这样的内联编码,所以
如何处理这种情况呢? />
TIA

- Alan Silver
(此行下面添加的任何内容与我无关)




-

Alan Silver

(此行下面添加的任何东西都与我无关)


为什么不使用内置的FormsAuthentication系统来处理您的

登录?这样,只需编辑web.config就可以轻松配置和限制对页面的访问。

。您还可以通过检查Context.User.IsAuthenticated来检查您的

用户是否已登录。


网上有很多关于如何使用的教程设置表格

身份验证。它可能比从头开始实施你自己的系统更安全,更快。


尼克......


Hello,

I am just planning a new ASP.NET site, and wondered about the best way
to handle the following common scenario...

The site has the option that registered users can log in and will have
access to extra features. The log in form is a simple username/password
affair that will appear in the margin of every page if they aren''t yet
logged in. If they are logged in, their user name will be shown, along
with a button to log out.

Now, suppose they try to log in. The log on form is posted to the log in
page, which verifies their username and password. If correct, they are
now logged in. If not, they are told so and asked for the details again,
or asked to register.

How do I deal with this on the rest of the site? I need to alter the
display on some pages so that messages like "If you were logged in you
would be able to do..." will be replaced with links to the extra
features. Similarly, certain pages will be imply unavailable if they are
not logged in.

In Classic ASP this was just a matter of setting a Session variable, say
"Username" to be non-blank. You could have a small include file, pulled
in on every relevant page, that contained code to check the Session
variable, and then redirect them away from the page if they weren''t
allowed there without logging in. If it was a page which they could see,
you would do something like...

<%If Session("Username")<>"" Then%>
<p>Click <a href="comment.asp">here</a> to add a comment.</p>
<%Else%>
<p>If you were logged in, you could add a comment.</p>
<%End If%>

Now I think that in-line coding like this isn''t allowed in ASP.NET, so
how do I handle the situation?

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

解决方案

Hi Alan,

In .NET page you can put a Hyperlink control and Label control in same
position. Then in codebehind switch them according to login or not

If Login Then
Hyperlink.Visible = True
Label.Visible = False
Else
Hyperlink.Visible = False
Label.Visible = True
End If
HTH

Elton Wang
el********@hotmail.com
"Alan Silver" wrote:

Hello,

I am just planning a new ASP.NET site, and wondered about the best way
to handle the following common scenario...

The site has the option that registered users can log in and will have
access to extra features. The log in form is a simple username/password
affair that will appear in the margin of every page if they aren''t yet
logged in. If they are logged in, their user name will be shown, along
with a button to log out.

Now, suppose they try to log in. The log on form is posted to the log in
page, which verifies their username and password. If correct, they are
now logged in. If not, they are told so and asked for the details again,
or asked to register.

How do I deal with this on the rest of the site? I need to alter the
display on some pages so that messages like "If you were logged in you
would be able to do..." will be replaced with links to the extra
features. Similarly, certain pages will be imply unavailable if they are
not logged in.

In Classic ASP this was just a matter of setting a Session variable, say
"Username" to be non-blank. You could have a small include file, pulled
in on every relevant page, that contained code to check the Session
variable, and then redirect them away from the page if they weren''t
allowed there without logging in. If it was a page which they could see,
you would do something like...

<%If Session("Username")<>"" Then%>
<p>Click <a href="comment.asp">here</a> to add a comment.</p>
<%Else%>
<p>If you were logged in, you could add a comment.</p>
<%End If%>

Now I think that in-line coding like this isn''t allowed in ASP.NET, so
how do I handle the situation?

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)



>Hi Alan,


In .NET page you can put a Hyperlink control and Label control in same
position. Then in codebehind switch them according to login or not
Thanks, that makes sense. What''s the best way to handle the actual log
in? You have a variable Login. Where is that set? Bear in mind the
logging in will be done on one page, and I then need to have the
information available to every page they visit afterwards. I could
understand a Session variable, but is there a better way?

Thanks for the reply.
If Login Then
Hyperlink.Visible = True
Label.Visible = False
Else
Hyperlink.Visible = False
Label.Visible = True
End If
HTH

Elton Wang
el********@hotmail.com
"Alan Silver" wrote:

Hello,

I am just planning a new ASP.NET site, and wondered about the best way
to handle the following common scenario...

The site has the option that registered users can log in and will have
access to extra features. The log in form is a simple username/password
affair that will appear in the margin of every page if they aren''t yet
logged in. If they are logged in, their user name will be shown, along
with a button to log out.

Now, suppose they try to log in. The log on form is posted to the log in
page, which verifies their username and password. If correct, they are
now logged in. If not, they are told so and asked for the details again,
or asked to register.

How do I deal with this on the rest of the site? I need to alter the
display on some pages so that messages like "If you were logged in you
would be able to do..." will be replaced with links to the extra
features. Similarly, certain pages will be imply unavailable if they are
not logged in.

In Classic ASP this was just a matter of setting a Session variable, say
"Username" to be non-blank. You could have a small include file, pulled
in on every relevant page, that contained code to check the Session
variable, and then redirect them away from the page if they weren''t
allowed there without logging in. If it was a page which they could see,
you would do something like...

<%If Session("Username")<>"" Then%>
<p>Click <a href="comment.asp">here</a> to add a comment.</p>
<%Else%>
<p>If you were logged in, you could add a comment.</p>
<%End If%>

Now I think that in-line coding like this isn''t allowed in ASP.NET, so
how do I handle the situation?

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)


Why not use the built in FormsAuthentication system to handle your
logins? This way you could configure and limit access to pages quite
easily by just editing web.config. You could also check to see if your
users are logged in by checking Context.User.IsAuthenticated.

There are many tutorials on the web for how to setup Forms
Authentication. It would probably be more secure and quicker than
implementing your own system from scratch.

Nick...


这篇关于如何判断用户是否已登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆