简体   繁体   English

在登录页面中使用页面用户身份名称

[英]Using Page User Identity Name In Login Page

I have a created a project in Visual Studio 2012 using the default template. 我使用默认模板在Visual Studio 2012中创建了一个项目。 I want each user to be able to login and be redirected to a different page. 我希望每个用户都可以登录并重定向到其他页面。 I have implemented code that works on the first user login. 我已经实现了可用于首次用户登录的代码。 However if I log out, then login with a different user, that user doesn't get redirected. 但是,如果我注销后再使用其他用户登录,则该用户不会被重定向。 When I debug the app, on the second login it come up blank on the Username. 当我调试应用程序时,在第二次登录时,它在用户名上显示为空白。 I have put the code in page_load event, is there a better event or better way of implementing this code? 我已将代码放在page_load事件中,是否有更好的事件或实现此代码的更好方法?

var Username = Page.User.Identity.Name;

if (Username == "You" || Username == "you")
{
    Response.Redirect("~/you.aspx");
}
else if (Username == "Me" || Username == "me")
{
    Response.Redirect("~/me.aspx");
}
else if (Username == "Them" || Username == "them")
{
    Response.Redirect("~/Them.aspx");
}

I have also tried this code as well, however none of the users are redirected once they logged in. The it looks to me like the aspx part of the code isn't fired after or @ login: 我也尝试过此代码,但是登录后没有任何用户重定向。在我看来,该代码的aspx部分在@或@登录后未触发:

Code Behind: 背后的代码:

public partial class Login : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void LoggedIn(object sender, EventArgs e)
    {
        //This is redirect the user to their page
        switch (Page.User.Identity.Name.ToLower())
        {
            case "me":
                Response.Redirect("~/me.aspx");
                break;
            case "you":
                Response.Redirect("~/you.aspx");
                break;
            case "them":
                Response.Redirect("~/them.aspx");
                break;
        }
    }
    protected void Unnamed6_Click(object sender, EventArgs e)
    {

    }
}

Aspx Code Here: 此处的Aspx代码:

<%@ Page Title="Log in" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="ChoresLists.Account.Login" %>
<%@ Register Src="~/Account/OpenAuthProviders.ascx" TagPrefix="uc" TagName="OpenAuthProviders" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <hgroup class="title">
        <h1><%: Title %>.</h1>
    </hgroup>
    <section id="loginForm">
        <h2>Use a local account to log in.</h2>
        <asp:Login runat="server" ViewStateMode="Disabled" RenderOuterTable="false" OnLoggedIn="LoggedIn">
            <LayoutTemplate>
                <p class="validation-summary-errors">
                    <asp:Literal runat="server" ID="FailureText" />
                </p>
                <fieldset>
                    <legend>Log in Form</legend>
                    <ol>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
                            <asp:TextBox runat="server" ID="UserName" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                        </li>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                            <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                        </li>
                        <li>
                            <asp:CheckBox runat="server" ID="RememberMe" />
                            <asp:Label runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
                        </li>
                    </ol>
                    <asp:Button runat="server" CommandName="Login" Text="Log in" OnClick="Unnamed6_Click" />
                </fieldset>
            </LayoutTemplate>
        </asp:Login>
        <p>
            <%--<asp:HyperLink runat="server" ID="RegisterHyperLink" ViewStateMode="Disabled">Register</asp:HyperLink>
            if you don't have an account.--%>
        </p>
    </section>

    <section id="socialLoginForm">

        <%--<h2>Use another service to log in.</h2>
        <uc:OpenAuthProviders runat="server" ID="OpenAuthLogin" />--%>
        <asp:Image ID="LoginImage" runat="server" ImageUrl="~/Images/102_0273.png" Width="450px" />
    </section>
</asp:Content>

Hey hey hey why you are using: 嘿嘿嘿为什么要使用:

if (Username == "You" || Username == "you")
{
    Response.Redirect("~/you.aspx");
}
if (Username == "Me" || Username == "me")
{
    Response.Redirect("~/me.aspx");
}
if (Username == "Them" || Username == "them")
{
    Response.Redirect("~/Them.aspx");
}

Instead of using: 而不是使用:

if (Username == "You" || Username == "you")
{
   Response.Redirect("~/you.aspx");
}
else if (Username == "Me" || Username == "me")
{
   Response.Redirect("~/me.aspx");
}
else if (Username == "Them" || Username == "them")
{
   Response.Redirect("~/Them.aspx");
}

Since URLs in IIS are typically case-insensitive you can just use this: 由于IIS中的URL通常不区分大小写,因此您可以使用以下命令:

Response.Redirect(String.Format("~/{0}.aspx", Page.User.Identity.Name)); Response.Redirect(String.Format(“〜/ {0} .aspx”,Page.User.Identity.Name));

As for why the second login isn't redirecting, we would need more information as to your page life cycle and if you are doing something like if (Page.IsPostBack) checks. 至于第二次登录为什么不重定向的原因,我们将需要有关页面生命周期以及是否正在执行if(Page.IsPostBack)检查之类的更多信息。 Are you even hitting this code when your second user logs in? 当您的第二个用户登录时,您甚至没有击中该代码?

You should probably be handling the LoggedIn event of the Login control rather than putting this in Page_Load. 您可能应该处理Login控件的LoggedIn事件,而不是将其放在Page_Load中。 Something like this: 像这样:

Markup: 标记:

<asp:Login id="Login1" runat="server" OnLoggedIn="LoggedIn"></asp:Login>

Code: 码:

protected void LoggedIn(object sender, EventArgs e)
{
    Response.Redirect(String.Format("~/{0}.aspx", Page.User.Identity.Name));
}

What you could do is something like this 你能做的就是这样

Add a OnLoggedIn event to your asp:Login page 将OnLoggedIn事件添加到您的asp:Login页面

    <asp:Login  OnLoggedIn="LoggedOn" .../>

Then have a method like this 然后有一个这样的方法

    protected void LoggedOn(object sender, EventArgs e)
    {
        switch (Page.User.Identity.Name.ToLower())
        {
            case "you":
                Response.Redirect("~you.aspx");
                break;
            case "me":
                Response.Redirect("~me.aspx");
                break;
            case "them":
                Response.Redirect("~them.aspx");
                break;
            default:
                Response.Redirect("Somewhere.aspx");
                break;
        }
    }

I finally figured out what I needed to do. 我终于弄清楚了我需要做什么。 I didn't need to change the login form at all. 我根本不需要更改登录表单。 The only thing that I needed to do was comment out this line in Web.Config file: 我唯一需要做的就是注释掉Web.Config文件中的这一行:

<forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/"/>

Now my logins work just fine. 现在,我的登录正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM