วันพุธที่ 31 กรกฎาคม พ.ศ. 2556

Oracle XE Connect in command line.

user command

sqlplus / as sysdba

enter for connect as sysdba

วันจันทร์ที่ 29 กรกฎาคม พ.ศ. 2556

Disable SimpleMembership in MVC 4

Today I need to use simple membership in web.config but SimpleMembership in MVC 4 is not simple.

First u need to custom Web.config like this
        <authentication mode="Forms">
            <forms loginUrl="~/Account/Login" timeout="2880" >
                <credentials passwordFormat="Clear">
                    <user name="test" password="test"/>
                </credentials>
            </forms>
        </authentication>

add this to AppSettings
<add key="enableSimpleMembership" value="false"/>

and in AccountController disable InitializeSimpleMembership
//[InitializeSimpleMembership]

now for Login use this for realy simple life.

        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login( LoginModel model, string returnUrl )
        {
            //if (ModelState.IsValid && WebSecurity.Login( model.UserName, model.Password, persistCookie: model.RememberMe ))
            //{
            //    return RedirectToLocal( returnUrl );
            //}
            if (!ValidateLogOn( model.UserName, model.Password ))
                return View( model );

            FormsAuthentication.SetAuthCookie( model.UserName, model.RememberMe );

            if (!String.IsNullOrEmpty( returnUrl ))
                return Redirect( returnUrl );
            else
                return RedirectToAction( "LogOn" );

            //// If we got this far, something failed, redisplay form
            //ModelState.AddModelError( "", "The user name or password provided is incorrect." );
            //return View( model );
        }

        //Add Custom Validate for validate user and pass in Web.config
        private bool ValidateLogOn( string userName, string passWord )
        {
            if (string.IsNullOrEmpty( userName ))
                ModelState.AddModelError( "username", "User name required" );

            if (string.IsNullOrEmpty( passWord ))
                ModelState.AddModelError( "password", "Password required" );

            if (ModelState.IsValid && !FormsAuthentication.Authenticate( userName, passWord ))
                ModelState.AddModelError( "_FORM", "Wrong user name or password" );

            return ModelState.IsValid;
        }
        //End Custom Validate

        //
        // POST: /Account/LogOff

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            //WebSecurity.Logout();

            //return RedirectToAction( "Index", "Home" );

            FormsAuthentication.SignOut();
            return RedirectToAction( "LogOn" );
        }

c# Assembly QualifiedName format and How to find.

Format is
NamespaceQualifiedTypeName, AssemblyName
 
How to get is
 
Type objType = typeof(System.Array);

        // Print the full assembly name.
        Console.WriteLine ("Full assembly name:\n   {0}.", 
                           objType.Assembly.FullName.ToString()); 

        // Print the qualified assembly name.
        Console.WriteLine ("Qualified assembly name:\n   {0}.", 
                           objType.AssemblyQualifiedName.ToString());  

วันจันทร์ที่ 1 กรกฎาคม พ.ศ. 2556

Integrate NLog in my MVC 4

use Nuget to get NLog to your MVC
current version is 2.0.1.2 ad 1/7/2555 (d/m/yyyy)

create file NLog.config and input this

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <variable name="appName" value="MOTEWorkflow" />
    <targets>
        <target name="console" xsi:type="ColoredConsole"
          layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />
        <target name="file" xsi:type="File" fileName="${basedir}/Logs/rrLog.log"
          layout="${date}: ${message}" />
    </targets>
    <rules>
        <logger name="*" minlevel="Info" writeTo="file" />
    </rules>
</nlog>

then when need log use like this

NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
logger.Error( "Test Error" );

this is simple but more configuration and details can be found here.
https://github.com/NLog/NLog/wiki