ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Page Redirection based on Login user department using Form Authentication in ASP.Net<p>Hi <a class="username" rel="micah">micah</a>,</p> <p>Please refer below sample.</p> <p>As per your condition can redirect different page.</p> <p>In this sample i am redirecting same page for all condition.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>Login.aspx</strong></p> <pre class="brush: html">&lt;asp:Login ID=&#34;Login1&#34; runat=&#34;server&#34; OnAuthenticate=&#34;ValidateUser&#34;&gt; &lt;/asp:Login&gt;</pre> <p><strong>Home.aspx</strong></p> <pre class="brush: html">&lt;div&gt; Welcome &lt;asp:LoginName ID=&#34;LoginName1&#34; runat=&#34;server&#34; Font-Bold=&#34;true&#34; /&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Label ID=&#34;lblLastLoginDate&#34; runat=&#34;server&#34; /&gt; &lt;asp:LoginStatus ID=&#34;LoginStatus1&#34; runat=&#34;server&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Configuration; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Configuration Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <p><strong>Login.aspx.cs</strong></p> <pre class="brush: csharp">protected void ValidateUser(object sender, EventArgs e) { int userId = 0; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("Validate_User")) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Username", Login1.UserName); cmd.Parameters.AddWithValue("@Password", Login1.Password); cmd.Connection = con; con.Open(); userId = Convert.ToInt32(cmd.ExecuteScalar()); con.Close(); } if (userId == 1) { FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet); Response.Redirect("Home.aspx"); } else if (userId == 2) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet); Response.Redirect("Home1.aspx"); } else if (userId == 3) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet); Response.Redirect("Home2.aspx"); } } }</pre> <p><strong>Home.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.Page.User.Identity.IsAuthenticated) { FormsAuthentication.RedirectToLoginPage(); } }</pre> <p><strong>VB.Net</strong></p> <p><strong>Login.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub ValidateUser(ByVal sender As Object, ByVal e As EventArgs) Dim userId As Integer = 0 Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand("Validate_User") cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.AddWithValue("@Username", Login1.UserName) cmd.Parameters.AddWithValue("@Password", Login1.Password) cmd.Connection = con con.Open() userId = Convert.ToInt32(cmd.ExecuteScalar()) con.Close() End Using If userId = 1 Then &nbsp; &nbsp; &nbsp; &nbsp; FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet) Response.Redirect("Home.aspx") ElseIf userId = 2 Then &nbsp; &nbsp; &nbsp; &nbsp; FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet) Response.Redirect("Home1.aspx") ElseIf userId = 3 Then &nbsp; &nbsp; &nbsp; &nbsp; FormsAuthentication.SetAuthCookie(Login1.UserName, Login1.RememberMeSet) Response.Redirect("Home2.aspx") End If End Using End Sub</pre> <p><strong>Home.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Me.Page.User.Identity.IsAuthenticated Then FormsAuthentication.RedirectToLoginPage() End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/D16StxG.gif" alt="" width="340" height="286" /></p> <p>Here UserId is return from procedure. So you need to return department from your procedure to redirect based on the department.</p>https://www.aspforums.net:443/Threads/626096/Page-Redirection-based-on-Login-user-department-using-Form-Authentication-in-ASPNet/https://www.aspforums.net:443/Threads/626096/Page-Redirection-based-on-Login-user-department-using-Form-Authentication-in-ASPNet/Tue, 13 Nov 2018 03:22:55 GMT