views:

92

answers:

4

I have two web applications. One is using windows authentication and the other is forms based. I need to add users to the later one from the first one? How do i achieve this?

A: 

can this be done .......

Shishya
+1  A: 

All the APIs for doing this exist as part of the Membership and Roles providers. From you windows authenticated application you can construct these objects and call the methods for things like creating users.

Documentation here. http://msdn.microsoft.com/en-us/library/tw292whz.aspx

keithwarren7
i have tried doing that from the windows authenticated application. but im not able to add as they are DIFFERENT applications.
Shishya
A: 

I got this to work.

What helped me was both applications use the same database. So basically what i did was when i need to acesss the FOrm Authenticated app, i pass the required information to do windows authentication in the query string.

Once i authenticate it using code behind logic, i tediously create a corresponding user in the authentication schema and it works.

 if (!String.IsNullOrEmpty(Request.QueryString["eid"]))
            {
                if (CheckSecureTokenLogin())
                {
                    String role = "NotAvailable";
                    if(!String.IsNullOrEmpty(Request.QueryString["rl"]))
                    {
                        role = Request.QueryString["rl"];
                    }
                    LoginDAO edao = new LoginDAO();
                    int employeeID = Int32.Parse(Request.QueryString["eid"]);
                    string username = edao.GetAspnetUsername(ID);
                    string password = edao.GetLoginInfo(ID).Password;
                    string emailaddress = edao.GetLoginInfo(ID).EmailAddress;
                    if (username == null)
                    {
                        username = edao.GetLoginInfo(ID).Username;
                        try
                        {
                            using (var transaction = new TransactionScope())
                            {
                                MembershipUser newUser = Membership.CreateUser(username, password, emailaddress);
                                if (!role.Equals("NotAvailable"))
                                {
                                    if (Roles.RoleExists(role))
                                    {
                                        if (!Roles.IsUserInRole(username, role))
                                        {
                                            Roles.AddUserToRole(username, role);
                                        }
                                        else
                                        {
                                            Response.Write("invalid role in query");
                                        }
                                    }
                                }
                                new LoginDAO().AddAspnetUserID(ID, new AspnetUserDAO().getUserID(username));
                                transaction.Complete();
                            }
                        }
                        catch (MembershipCreateUserException ex)
                        {
                            LogDAO.AddExceptionEntry(ex.ToString());
                            MessageBox(ex.StatusCode.ToString());
                            throw;
                        }
                    }

                    if (Membership.ValidateUser(username,password))
                    {
                        if (Roles.RoleExists(role))
                        {
                            if (!Roles.IsUserInRole(username,role))
                            {
                                Roles.AddUserToRole(username, role);
                            }

                            FormsAuthentication.SetAuthCookie(username, false);
                            if (!String.IsNullOrEmpty(GetHomeURL(role)))
                            {
                                Response.Redirect(GetHomeURL(role));
                            }
                            else
                            {
                                Response.Write("role not known");
                            } 
                        }
                        else
                        {
                            Response.Write("invalid role in query");
                        }
                    }
                }
Shishya
A: 

if (!String.IsNullOrEmpty(Request.QueryString["eid"])) { if (CheckSecureTokenLogin()) { String role = "NotAvailable"; if(!String.IsNullOrEmpty(Request.QueryString["rl"])) { role = Request.QueryString["rl"]; } LoginDAO edao = new LoginDAO(); int employeeID = Int32.Parse(Request.QueryString["eid"]); string username = edao.GetAspnetUsername(ID); string password = edao.GetLoginInfo(ID).Password; string emailaddress = edao.GetLoginInfo(ID).EmailAddress; if (username == null) { username = edao.GetLoginInfo(ID).Username; try { using (var transaction = new TransactionScope()) { MembershipUser newUser = Membership.CreateUser(username, password, emailaddress); if (!role.Equals("NotAvailable")) { if (Roles.RoleExists(role)) { if (!Roles.IsUserInRole(username, role)) { Roles.AddUserToRole(username, role); } else { Response.Write("invalid role in query"); } } } new LoginDAO().AddAspnetUserID(ID, new AspnetUserDAO().getUserID(username)); transaction.Complete(); } } catch (MembershipCreateUserException ex) { LogDAO.AddExceptionEntry(ex.ToString()); MessageBox(ex.StatusCode.ToString()); throw; } }

                if (Membership.ValidateUser(username,password))
                {
                    if (Roles.RoleExists(role))
                    {
                        if (!Roles.IsUserInRole(username,role))
                        {
                            Roles.AddUserToRole(username, role);
                        }

                        FormsAuthentication.SetAuthCookie(username, false);
                        if (!String.IsNullOrEmpty(GetHomeURL(role)))
                        {
                            Response.Redirect(GetHomeURL(role));
                        }
                        else
                        {
                            Response.Write("role not known");
                        } 
                    }
                    else
                    {
                        Response.Write("invalid role in query");
                    }
                }
            }
aaaaaaaa