Hi skp,
You can make use of Session state to save the user name and id and then retrieve after logged in.
Check this example. Now please take its reference and correct your code.
Refering the below article i have created an example.
Check this example. Now please take its reference and correct your code.
HTML
Login.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<link type="text/css" rel="stylesheet" media="screen" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css' />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<link rel="stylesheet" href="login.css" />
<style type="text/css">
body
{
background: -webkit-linear-gradient(left, #1143a6, #00c6ff);
}
</style>
<script type="text/javascript">
var LoginApp = angular.module('LoginApp', []);
LoginApp.controller('LoginController', ['$scope', '$http', '$window', function ($scope, $http, $window) {
$scope.submit = function () {
var UserData = { username: $scope.Username, password: $scope.Password }
$http
.post('Login.aspx/ValidateUser', UserData)
.success(function (data, status, headers, config) {
if (data.d > 0) {
$window.location.href = 'success.aspx';
} else {
$scope.message = data.d;
}
}).error(function (data, status, headers, config) {
$scope.message = data.d;
});
}
} ]);
</script>
</head>
<body ng-app="LoginApp" ng-controller="LoginController">
<section class="container">
<div class="main_cont register">
<div class="login-panel">
<h3 class="login-heading"></h3>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<input type="text" class="form-control" ng-model="Username" name="Name" placeholder="Username" value="" autocomplete="off" required/>
</div>
<div class="form-group">
<input type="password" class="form-control" ng-model="Password" name="Password" placeholder="Password" value="" autocomplete="off" required/>
</div>
<div class="float-right">
<input type="button" class="btn btn-primary" ng-click="submit()" value="Login" />
<div style="color: red">{{message}}</div>
</div>
</div>
</div>
</div>
</div>
</section>
</body>
</html>
Success.aspx
C#
<div>
<h2>
User Name :
<%=this.UserName%>
<br />
User Id :
<%=this.UserId%>
</h2>
</div>
VB.Net
<div>
<h2>
User Name :
<%=Me.UserName%>
<br />
User Id :
<%=Me.UserId%>
</h2>
</div>
Code
Login Page
C#
[System.Web.Services.WebMethod]
public static string ValidateUser(string username, string password)
{
string status = "";
int userId = 0;
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("Validate_User"))
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username", username);
cmd.Parameters.AddWithValue("@Password", password);
cmd.Connection = con;
con.Open();
userId = Convert.ToInt32(cmd.ExecuteScalar());
System.Web.HttpContext.Current.Session["UserName"] = username;
System.Web.HttpContext.Current.Session["UserId"] = userId;
con.Close();
}
switch (userId)
{
case -1:
status = "Username and/or password is incorrect.";
break;
case -2:
status = "Account has not been activated.";
break;
default:
status = userId.ToString();
break;
}
}
return status;
}
VB.Net
<System.Web.Services.WebMethod>
Public Shared Function ValidateUser(ByVal username As String, ByVal password As String) As String
Dim status As String = ""
Dim userId As Integer = 0
Dim constr As String = System.Configuration.ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(constr)
Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("Validate_User")
cmd.CommandType = System.Data.CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Username", username)
cmd.Parameters.AddWithValue("@Password", password)
cmd.Connection = con
con.Open()
userId = Convert.ToInt32(cmd.ExecuteScalar())
System.Web.HttpContext.Current.Session("UserName") = username
System.Web.HttpContext.Current.Session("UserId") = userId
con.Close()
End Using
Select Case userId
Case -1
status = "Username and/or password is incorrect."
Case -2
status = "Account has not been activated."
Case Else
status = userId.ToString()
End Select
End Using
Return status
End Function
Success Page
C#
public string UserName { get; set; }
public string UserId { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null && Session["UserId"] != null)
{
UserName = Session["UserName"].ToString();
UserId = Session["UserId"].ToString();
}
}
VB.Net
Public Property UserName As String
Public Property UserId As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Session("UserName") IsNot Nothing AndAlso Session("UserId") IsNot Nothing Then
UserName = Session("UserName").ToString()
UserId = Session("UserId").ToString()
End If
End Sub
Screenshot