1

ASP.Net と C# は初めてです。C# でログインした後、HTML ファイルにリダイレクトしたい。この HTML は SuperGIS サーバーの開始ページです。

私はこのようなものを使用しています:

    <%@ Page Language="C#" Debug="true" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">

    protected void btnlogin_Click(object sender, EventArgs e)
    {
    if(some code to check username and password exist in DB)
        Response.Redirect("http://localhost/AddMarker/MapEditor.htm");
        else
         Response.Redirect("http://localhost/AddMarker/Login.aspx");

    }

    </script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login Page</title>
</head>
<body>
    <form id="form1" runat="server">

    <div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        Sign In<br />
        <br />
<asp:Label ID="lblUsername" runat="server" Text="Username"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate = "txtUsername"
            ErrorMessage="This Feild is Mendetory"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Label ID="lblPassword" runat="server" Text="Password"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="btnlogin" runat="server" Text="Login" onclick="btnlogin_Click" 
Width="47px" />
&nbsp;
<br />

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</div>

    </form>
    <a href="forgetpass.aspx">Forgt Password</a><br>
    <a href="Create.aspx">New User</a>
</body>
</html>

このコードは、目的の宛先にリダイレクトします。ただし、その時点での URL は
-->"http://localhost/AddMarker/MapEditor.htm"になります。

これはコードによるものです: Response.Redirect("http://localhost/AddMarker/MapEditor.htm");

したがって、誰でもこの URL をコピーして、認証なしでこのサービスにアクセスできます。

正しい ID とパスワードを指定しないと使用できない動的 URL を作成できますか。

私を助けてください。前もって感謝します。

4

4 に答える 4

1

You can use session state to store if a user is authenticated.

if(some code to check username and password exist in DB) 
{
    HttpContext.Current.Session("UserIsAuthenticated") = True
    Response.Redirect("http://localhost/AddMarker/MapEditor.htm"); 
}

You can set your session time in web.config so that you only stay authenticated for a certain amount of time. Then in the load event of the pages where you need to be authenticated use this:

if (HttpContext.Current.Session("UserIsAuthenticated") == null)
   //User needs to login
   Response.Redirect("Login.aspx")
else if(!HttpContext.Current.Session("UserIsAuthenticated"))
   //User needs to login
   Response.Redirect("Login.aspx")

You could also do this with cookies, but I would recomend not doing this where possible due to the new leigislations with cookies.

于 2012-10-09T11:16:42.413 に答える
0

これはhttpハンドラーを使用して実行できます。詳細については、こちらをご覧ください。

  1. http://msdn.microsoft.com/en-us/library/bb398986(v=vs.100).aspx
  2. http://codebetter.com/karlseguin/2006/05/25/httphandlers-%E2%80%93-learn-them-use-them/
  3. http://www.aspnettutorials.com/tutorials/advanced/httphandler-aspnet2-csharp.aspx
于 2012-10-09T11:14:17.023 に答える
0

MapEditor.htm で ASP .Net を使用してアクセスを確認するのが最善の方法だと思います。ユーザーが認証されると、フラグをセッションに保存します。次に、MapEditor で常にこのフラグをチェックします。aspx

于 2012-10-09T11:21:26.320 に答える
0

ログイン状態チェックを Global.asax に配置する必要があります。Application_BeginRequest などのイベントの 1 つを使用して、ユーザーがログインしているかどうかを確認し、ログインしていない場合はリダイレクトします。ユーザーがプルアップしようとしているページがログイン ページではないことを確認する必要がありますが、それ以外の場合は機能しません。

更新:これは、うまく機能するマスターページで使用したコードです。マスターページを使用していない場合は、Global.asax を適切な場所に配置すると機能します。

このコードは、サイトが Windows 認証を使用しているため、ユーザーが特定のドメイン グループに属しているかどうかを確認することを目的としています。ユーザーを検証するために必要な方法は何でも交換できます。また、 NoAccess ページで AccessException bool が true に設定されているため、実際に表示されます。最後に、チェックはキャッシュされるため、チェックし続ける必要はありません。

public bool AccessException;

protected void Page_Load(object sender, EventArgs e)
{
    UpdateNavigation();

    if (!HasAccess() && !AccessException)
    {
        Response.Redirect("~/NoAccess.aspx", true);
    }
}

/// <summary>
/// Check domain group membership to make sure the user is allowed to use the web site; redirect if not.
/// </summary>
/// <returns>True if the user has access to the site</returns>
private bool HasAccess()
{
    if (Page.Cache["hasAccess"] == null)
    {
        List<string> userDomainGroups = Common.GetUserDomainGroups(Request.LogonUserIdentity);
        string permittedGroups = ConfigurationManager.AppSettings["AllowedGroups"];

        Page.Cache["hasAccess"] = userDomainGroups.Where(permittedGroups.Contains).Count() != 0; ;
    }

    return (bool)Page.Cache["hasAccess"];
}
于 2012-10-09T11:23:53.743 に答える