Thursday, August 27, 2015

HttpContext.Current.User.Identity.Name returns null or empty string value

Introduction
Here I will explain how to solve problem of HttpContext.Current.User.Identity.Name returns null or empty string value.

Dsecription

I have one requirement like getting system username based on that check the permissions of user if user has sufficient permission I need to allow that person to view the particular page otherwise I don’t want give chance to show the page for that I have written code to get username like this 


string username = HttpContext.Current.User.Identity.Name.ToLower();
After that I run the application un expectdly I got username as null at that time again I checked my code to get username correctly at that time I found the mistake that is in web.config I write the authentication like this 

<authentication mode="Forms"/>
 Because of authentication mode="Forms" I got username is null that’s why I change th e authentication mode to Windows like this 

<authentication mode="Windows"/>
Again I run the application and checked for username now I got the username successfully.


Monday, June 22, 2015

Get All Logged in Users Asp.net MVC

[HttpPost]

        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (MembershipService.ValidateUser(model.UserName, model.Password))
                {
                    FormsService.SignIn(model.UserName, model.RememberMe);
                    if (HttpRuntime.Cache["LoggedInUsers"] != null) //if the list exists, add this user to it
                    {
                        //get the list of logged in users from the cache
                        List loggedInUsers = (List)HttpRuntime.Cache["LoggedInUsers"];
                        //add this user to the list
                        loggedInUsers.Add(model.UserName);
                        //add the list back into the cache
                        HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers;
                    }
                    else //the list does not exist so create it
                    {
                        //create a new list
                        List loggedInUsers = new List();
                        //add this user to the list
                        loggedInUsers.Add(model.UserName);
                        //add the list into the cache
                        HttpRuntime.Cache["LoggedInUsers"] = loggedInUsers;
                    }
                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }
            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public ActionResult LogOff()
        {

            string username = User.Identity.Name; //get the users username who is logged in

            if (HttpRuntime.Cache["LoggedInUsers"] != null)//check if the list has been created
            {
                //the list is not null so we retrieve it from the cache
                List loggedInUsers = (List)HttpRuntime.Cache["LoggedInUsers"];
                if (loggedInUsers.Contains(username))//if the user is in the list
                {
                    //then remove them
                    loggedInUsers.Remove(username);
                }
                // else do nothing

            }

Saturday, June 6, 2015

Auto Resize TextBox

Auto Resize text Box
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .clsTxt {
            width: 200px;
            min-height: 25px;
            max-height: 200px;
            resize: none;
        }
    </style>
    <script>
        function resizeTextBox(txt) {
            txt.style.height = "1px";
            txt.style.height = (1 + txt.scrollHeight) + "px";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Description: </td>
                    <td>                       
                        <asp:TextBox ID="txtDescription" CssClass="clsTxt" runat="server"onkeyup="resizeTextBox(this)" TextMode="MultiLine"></asp:TextBox>
                        <%--<textarea class="clsTxt" onkeyup="resizeTextBox(this)"></textarea>--%>
                    </td>
                </tr> 
            </table> 
        </div>
    </form>
</body>
</html>




Note: In this article I have demonstrated the concept using asp.net multiline textbox. But if you want to implement it on HTML textarea then use 
<textarea class="clsTxt" onkeyup="resizeTextBox(this)"></textarea>
Instead of
<asp:TextBox ID="txtDescription" CssClass="clsTxt" runat="server" onkeyup="resizeTextBox(this)"TextMode="MultiLine"></asp:TextBox>