Web配置中的身份验证表单 [英] authentication forms in web config

查看:106
本文介绍了Web配置中的身份验证表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录表单,成功登录后我已将用户定向到Web表单.使用活动目录进行身份验证.然后我要获取登录的用户名并始终检查用户是否登录在显示页面之前,在有向页面中.在Web配置中,我有:

I have a login form and after successful login i have directed the user to a web form.The authentication is with the active directory.Then i want to get the loged in user name and alway perform a check if the user is login in the directed page before showing up the page.In web config i have:

<authentication mode="Forms">
                <forms loginUrl="Login.aspx" timeout="2880" />
        </authentication>



在定向页面中,我有:



in the directed page page i have:

string currentUserName = HttpContext.Current.User.Identity.Name;



 protected void Page_Load(object sender, EventArgs e)
 {

    if (HttpContext.Current.User.Identity.IsAuthenticated)
     {



     }

     else
     {
         Response.Redirect("Login.aspx");
         // action for non-authenticated users.

     }



}
问题是,当前用户名始终为null,而HttpContext.Current.User.Identity.IsAuthenticated始终为false.我该如何更正...任何更好的方法

谢谢



}
the problem is the that the currentuser name is always null and HttpContext.Current.User.Identity.IsAuthenticated is always false.How can i correct this...any better approch

Thanks

推荐答案

仅当用户通过身份验证时,您才获得用户名:

You get username only when user is authenticated:

string userName = "";
if (HttpContext.Current.User.Identity.IsAuthenticated)
 userName = HttpContext.Current.User.Identity.Name;


嘿,
您是否曾经在web.config中添加了对成员资格提供者的引用?另外,如果您使用的是ASP.Net MVC,则大多数情况下会出现此错误.让我们知道您是否正在使用它.

如何使用成员资格提供程序 [演练:创建具有成员资格和用户登录名的网站 [ ^ ]< ;<进一步阅读.

希望对您有所帮助
Hey there,
have you used added a reference to the membership provider in the web.config? Also, you get this error most of the time if you are using ASP.Net MVC. Let us know if you are using that.

How to use Membership Provider[^] is a great article on how to embed the membership functionality into your website. Furthermore, if you read this article thoroughly, you do not have to check whether the user is authenticated in the code behind. You can do this by adding a web.config for each directory of the website and allow/deny access to users, roles, etc.

Walkthrough: Creating a Web Site with Membership and User Login[^] << For further reading.

Hope this helps, regards


这篇关于Web配置中的身份验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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