Click here to Skip to main content
15,943,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am creating the web service authentication. When the user login with user name and password. Web server will give the token to the client to request the web service with that token. I would like to use the cookie to save the token in order to let the web service to authenticate whether the client's token is correct or not.
The following is my code. please give me example code, if you can.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;

namespace AuthWebApplication
{
    using System.Security.Cryptography;

    /// summary
    /// Summary description for WebService1
    /// /summary
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        public WebService1()
        {

            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
        //public AuthHeader SoapAuthentication;

        [WebMethod(Description = "A sample Web Method to demonstrate a simple web Service Authentication using SOAP Headers")]
        public string SampleWebMethod(string Username, String Password)
        {

            if (Username == "demo" && Password == "123")
            {

                string token = Guid.NewGuid().ToString();

                HttpRuntime.Cache.Add(token, Username, null,

                System.Web.Caching.Cache.NoAbsoluteExpiration,

                TimeSpan.FromMinutes(60),

                System.Web.Caching.CacheItemPriority.NotRemovable,

                null);

                return token + "is an Authenticated User to access the Web Method";
              //  return Username + " is an Authenticated User to access the Web Method";
            }
            else
            {
                return "Access Denied for " + Username;
            }

        }

}

}
Posted
Updated 8-Sep-11 18:50pm
v3

You can use Response object to store the content in cookie.
C#
Response.Cookies["userInfo"]["Username"] = "demo";
Response.Cookies["userInfo"]["Password"] = "123";
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);
 
Share this answer
 
C#
string userData ="123";

C#
// Create a cookie authentication ticket.
               ticket = new FormsAuthenticationTicket(
                   1,                                // version
                   Test,                             // user name
                   DateTime.Now,                     // issue time
                   DateTime.Now.AddDays(3),          // expires every hour
                   false,                            // don't persist cookie
                   userData                          // user data
                   );
               // Encrypt the ticket
               String cookieStr = FormsAuthentication.Encrypt(ticket);

               // Send the cookie to the client
               HttpContext.Current.Response.Cookies["Testdata"].Value = cookieStr;
               HttpContext.Current.Response.Cookies["Testdata"].Path = "/";
                HttpContext.Current.Response.Cookies["Testdata"].Expires   = DateTime.Now.AddMonths(3);
 
Share this answer
 
Comments
Wai Mar Khaing 9-Sep-11 2:50am    
what is FormsAuthenticationTicket? is it web service name or ? I got an error when I write the "FromsAuthenticationTicket"
Anuja Pawar Indore 9-Sep-11 3:56am    
FormsAuthenticationTicket ticket;

Declare this at the top, we are using form authentication.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900